If else Exercises
C program to check whether a character is Uppercase or Lowercase
Write a C program to input character from user and check whether character is uppercase or lowercase alphabet using if else. How to check uppercase and lowercase using if else in C programming. Logic to check uppercase and lowercase alphabets in C program.
Example
Input
Input character: C
Output
'C' is uppercase alphabet
Required knowledge
Basic C programming, Relational operators, Logical operators, If else
Logic to check uppercase and lowercase alphabets
Step by step descriptive logic to check uppercase and lowercase alphabets.
1.Input a character from user. Store it in some variable say ch.
2.Character is uppercase alphabet if(ch >= 'A' and ch <= 'Z').
3.Character is lowercase alphabet if(ch >= 'a' and ch <= 'z').
4.If none of the above conditions met, then character is not alphabet.
Program to check uppercase or lowercase alphabets
/** * C program to check whether a character is uppercase or lowercase */ #include <stdio.h> int main() { char ch; /* Input character from user */ printf("Enter any character: "); scanf("%c", &ch); if(ch >= 'A' && ch <= 'Z') { printf("'%c' is uppercase alphabet.", ch); } else if(ch >= 'a' && ch <= 'z') { printf("'%c' is lowercase alphabet.", ch); } else { printf("'%c' is not an alphabet.", ch); } return 0; }
You can also use inbuilt library function isupper() and islower() to check uppercase and lowercase alphabets respectively. These functions are present in ctype.h header file. Both function returns 1 if given character is uppercase or lowercase respectively otherwise returns 0.
Program to check uppercase or lowercase characters using library functions
/** * C program to check whether a character is uppercase * or lowercase using inbuilt library functions */ #include <stdio.h> #include <ctype.h> /* Used for isupper() and islower() */ int main() { char ch; /* Input character from user */ printf("Enter any character: "); scanf("%c", &ch); if(isupper(ch)) { printf("'%c' is uppercase alphabet.", ch); } else if(islower(ch)) { printf("'%c' is lowercase alphabet.", ch); } else { printf("'%c' is not an alphabet.", ch); } return 0; }
The statement if(isupper(ch)) is equivalent to if(isupper(ch) == 1).
Output
Enter any character: C
'C' is uppercase alphabet.