C Program to Check Whether A Character is an Upper Case or Lower Case – In this distinct article, we will detail in on the several ways to check whether a character is an upper case or lower case.
Check out the blog to access the sample programs and suitable examples. The compiler has been added as well for you to execute the various programs.
The ways in which the character is checked for being an upper case or lower case are as follows:
- Using Standard Method
- Using User-Defined Method
- Using Pointers
As you all know, in the English language, there are a couple of cases of the alphabets. They are known as Upper Case and Lower Case. This is how both of those look like:
As you can see, the left character is the Upper Case A and the right character is the Lower Case a. This specific article deals with the C programs to distinguish between both of these. The various ways are as follows:
Using Standard Method
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
#include <stdio.h> int main() { char ch; printf("Enter the alphabet:"); scanf("%c",&ch); if(ch>=65 && ch<=90) printf("upper case"); else if(ch>=97 && ch<=122) printf("lower case"); else printf("Invalid input"); return 0; } |
1 2 |
Enter the alphabet:g lower case |
Using User-Defined Function
1) Read the entered alphabet and store in the variable ch, using scanf function.
2)Pass the alphabet to the user-defined function lucase.
3)lucase function prints “uppercase” if entered alphabet ASCII value is >=65 and <=90,
prints “Lowercase” if entered alphabet ASCII value is >=97 and <=122,otherwise it prints invalid input.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
#include <stdio.h> void lucase(char ch); int main() { char ch; printf("Enter the alphabet:"); scanf("%c",&ch); lucase(ch); return 0; } void lucase(char ch) { if(ch>=65 && ch<=90) printf("upper case"); else if(ch>=97 && ch<=122) printf("lower case"); else printf("Invalid input"); } |
Output:
1 2 |
Enter the alphabet:G upper case |
Using Pointers
1)Pass the address of entered alphabet to the function lucase as lucase(&ch).
2)Then the pointer variable *ch of lucase(char *ch)contains the ASCII value of the entered alphabet.
3)If the ASCII value of ch >=65 and <=90 then it prints “upper case”, if ASCII value of ch>=97 and <=122 then it prints “lower case” otherwise it prints “invalid input”.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
#include <stdio.h> void lucase(char *ch); int main() { char ch; printf("Enter the alphabet:"); scanf("%c",&ch); lucase(&ch); return 0; } void lucase(char *ch) { if(*ch>=65 && *ch<=90) printf("upper case"); else if(*ch>=97 && *ch<=122) printf("lower case"); else printf("Invalid input"); } |
1 2 |
Enter the alphabet:G upper case |