Skip to content

Instantly share code, notes, and snippets.

@ZacBlanco
Last active October 3, 2016 01:27
Show Gist options
  • Save ZacBlanco/f4c3e224f4667011060e6a91bd147188 to your computer and use it in GitHub Desktop.
Save ZacBlanco/f4c3e224f4667011060e6a91bd147188 to your computer and use it in GitHub Desktop.
Characters in C
//Detect if a character is uppercase
int ALPHA_DIFF = 'a' - 'A';
//Determine if a character is uppercase, lowercase, or non-alphabetic.
int isUppercase(char a) {
if (a >= 'a' && a <= 'z') {
//a is lowercase
return 0;
} else if (a >= 'A' && a <= 'Z') {
//a is uppercase
return 1;
} else {
//a is not alphabetic
return -1;
}
}
//Convert a character to uppercase if it's already a lowercase.
char convertToUpper(char a) {
if (isUppercase(a)) {
return a; //Already uppercase
} else if (isUppercase(a) == 0) {
return a + ALPHA_DIFF;
} else {
return NULL;
}
}
char convertLower(char a) {
if (isUppercase(a)) {
return a - ALPHA_DIFF;
} else if (isUppercase(a) == 0) {
return a; //Already lowercase
} else {
return NULL;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment