Write a C program to check whether a character is uppercase or lowercase alphabet using if else . How to check uppercase and lowercase using if else in C programming. Program to enter a character from user and check whether the character is uppercase or lowercase alphabet without using in built library function. Logic to check uppercase and lowercase alphabets in C program.
Example
Input
Input character: C
Output
C is uppercase alphabet
Required knowledge
Logic to check uppercase and lowercase alphabets
If you are following my previous posts then you probably know the logic of this program. In previous posts I already explained logic for lower and uppercase alphabets. Let us recall the definitions for lowercase and uppercase alphabets once again.
- Lowercase alphabets are the characters in between a-z (ASCII 97-122).
- Uppercase alphabets are the character in between A-Z (ASCII 65-90).
Read the list of ASCII character values.
Using the above definition we can write a step by step descriptive logic -
- Read a character from user and store it in some variable say ch.
- Check condition for uppercase alphabets i.e. if ch >= 'A' and ch <= 'Z'. Then the given character is uppercase alphabet.
- Check condition for lowercase alphabets i.e. if ch >= 'a' and ch <= 'z'. Then the given character is lowercase alphabet.
Let us code the solution for this program.
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;
/* Read 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;
}
Note: You can also use inbuilt library function isupper() and islower() to check whether a character is uppercase or lowercase respectively. These functions are present under ctype.h header file. Both the functions returns 1 if the 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;
/* Reads a 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;
}
Enter any character: C C is uppercase alphabet.
Happy coding ;)
You may also like
- If else programming exercises and solutions.
- C program to convert uppercase string to lowercase.
- C program to convert lowercase string to uppercase.
- C program to check whether a character is alphabet or not.
- C program to check whether a character is vowel or consonant.
- C program to check whether a character is alphabet, digit or any special character.