Search⌘ K
AI Features

Discussion: On the Case

Explore how to manipulate letter cases in C programming by using bitwise operators and understanding ASCII values. Learn to flip bits to convert uppercase letters to lowercase and vice versa efficiently without relying solely on standard functions.

Run the code

Now, it's time to execute the code and observe the output.

C
#include <stdio.h>
int main()
{
char a;
for( a='A'; a<='Z'; a++ )
putchar( a | 0x20 );
putchar('\n');
for( a='a'; a<='z'; a++ )
putchar( a & 0xdf );
putchar('\n');
return(0);
}

Understanding the output

Two lines are output:

abcdefghijklmnopqrstuvwxyz
ABCDEFGHIJKLMNOPQRSTUVWXYZ
Code output

The uppercase letters are converted into lowercase. Lowercase letters are converted to uppercase.

Explanation

Most C programmers rush to the ctype functions when letters need case conversions. The toupper() and tolower() ...