Ans:
The character constant 'a' has the value 97 , and for 'b' the value is 98 ( i.e ) 'a' + 1 = b, the expression 'b' + 1 has the value 'c' and so on. Consider the expression 'A' - 'a' is same as 'B' - 'b' , which is same as 'C' - 'c', and so on. Hence, if the variable x has a value of lowercase letter, then the expression x + 'A' - 'a' has the value of the corresponding upper case letter.
void main( ){
int x;
while ((x=getchar( ))!='\n' )
if ( x>= 'a' && x<= 'z' )
putchar(x+'A'-'a');
else
putchar(x);
}
The same program can also be written using "toupper( )" function by including the library "ctype.h".
void main( ){
char s;
while((s=getchar( ))!='\n' )
if(islower(s))
printf("%c",toupper(s));
else
putchar(s);
}
The character constant 'a' has the value 97 , and for 'b' the value is 98 ( i.e ) 'a' + 1 = b, the expression 'b' + 1 has the value 'c' and so on. Consider the expression 'A' - 'a' is same as 'B' - 'b' , which is same as 'C' - 'c', and so on. Hence, if the variable x has a value of lowercase letter, then the expression x + 'A' - 'a' has the value of the corresponding upper case letter.
void main( ){
int x;
while ((x=getchar( ))!='\n' )
if ( x>= 'a' && x<= 'z' )
putchar(x+'A'-'a');
else
putchar(x);
}
The same program can also be written using "toupper( )" function by including the library "ctype.h".
void main( ){
char s;
while((s=getchar( ))!='\n' )
if(islower(s))
printf("%c",toupper(s));
else
putchar(s);
}
Categories:
program questions
This article is a bit of a crap...
First: The first algorithm is false... it says if() putchar(x).. else putchar(x) so we don't change anything at any moment to the string.
Second: The method using toupper() is generally not what is asked. It is asked to be smart. lowercase -> uppercase is just about changing a bit in the ASCII code, the second, to be exact, changing it from 1 (lowercase) to 0 (uppercase):
You can do that by substracting 32 but you will have to check for already-uppercased chars like you already do. Binary operators will save us this waste of time:
char uppercasechar = chr(ord(lowercasechar) & (127-32))
Have a nice day.