Last active
May 31, 2017 07:57
-
-
Save gon1332/59b727f855bf936f90ce04481deacc53 to your computer and use it in GitHub Desktop.
Check whether an ascii character is a letter or not.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* Thanks to my friend Chrysostomos for the solution. | |
*/ | |
#include <stdio.h> | |
int bin_search(int key, int left, int right) | |
{ | |
while (left <= right) { | |
int middle = (left + right) / 2; | |
if (middle < key) left = middle + 1; | |
else if (middle > key) right = middle - 1; | |
else return middle; | |
} | |
return -1; | |
} | |
int main(int argc, char *argv[]) | |
{ | |
if (argc != 2 || argv[1][1] != '\0') { | |
puts("Usage: isletter <character>"); | |
return 1; | |
} | |
int ret = bin_search(argv[1][0], 'A', 'Z') | |
+ bin_search(argv[1][0], 'a', 'z'); | |
if (ret < 0) puts("Not a letter."); | |
else puts("It is a letter indeed."); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment