PDA

View Full Version : Upper Case to Lower Case and vise-versa in C

Lippy
01-31-2005, 12:20 AM
How do I convert from an Uppercase letter to a Lowercase letter in C??

Thanks for the help,
Lippy

cfc
01-31-2005, 12:47 AM
a quick google search for C "upper case" "lower case" (http://www.google.ca/search?hl=en&client=firefox-a&rls=org.mozilla%3Aen-US%3Aofficial&q=C+%22upper+case%22+%22lower+case%22&btnG=Search&meta=) yielded:

tolower (http://www.thinkage.ca/english/gcos/expl/c/lib/tolowe.html)

and

toupper (http://www.thinkage.ca/english/gcos/expl/c/lib/touppe.html)

Lippy
01-31-2005, 01:03 AM
Hey this is great, thanks

anyone wouldn't by any chance know how to test to see if the letter is either upper case or lower case?

Lippy

cfc
01-31-2005, 01:29 AM
wouldn't it be as simple as:

if (toupper((int)mychar) == (int)mychar) {
// then it's upper case
} else {
// then it's lower case
}

? :confused:

I can't remember if integer type conversion from characters is implicit in C, so I edited it to add the integer typecasting :)

Dr. Evil
01-31-2005, 04:14 PM
A quicker way to do that would be to create a definition that tests the range, in example:

#define IS_UPPER(c) ((c >= 'A') && (c <= 'Z'))
#define IS_LOWER(c) ((c >= 'a') && (c <= 'z'))

char input = getchar();

if(IS_UPPER(input)) puts("Entered character is upper case");
else if(IS_LOWER(input)) puts("Entered character is lower case");
else puts("Entered character is not a letter");