In this tutorial, we will be learning how to write a C program to check whether a given character is uppercase or lowercase using if-else. Also, We will see examples and algorithms for a better understanding of the problem.
Problem Statement
Write a C program that checks whether a given character is uppercase or lowercase. Here Program will take a character as input. If the Given character is in the uppercase output should printed as “The character is uppercase”. And if the Given character is in lowercase, the output should be printed as “The character is lowercase”.
Examples
Enter a character: A
Output: The character A is uppercase.
Enter a character: b
Output: The character b is lowercase.
Enter a character: 7
Output: Input is invalid
In the below program we are using if else statement of the C programming
Algorithm
- Start the program.
- Declare a variable
character
of typechar
to store the input character. - Prompt the user to enter a character by printing the message “Enter a character: “.
- Read the input character from the user and store it in the
character
variable usingscanf()
. - Use an if-else statement to check the case of the input character:
- If
character
is between ‘A’ and ‘Z’, print the message “The character [character] is uppercase.” - If
character
is between ‘a’ and ‘z’, print the message “The character [character] is lowercase.” - Otherwise, print the message “The input is invalid”
- If
- End the program.
Program to check whether a given character is uppercase or lowercase
#include <stdio.h>
int main() {
char character;
printf("Enter a character: ");
scanf(" %c", &character);
if (character >= 'A' && character <= 'Z') {
printf("The character %c is uppercase.\n", character);
}
else if (character >= 'a' && character <= 'z') {
printf("The character %c is lowercase.\n", character);
}
else {
printf("The input %c is invalid.\n", character);
}
return 0;
}
Output 1
Enter a character: h
The character h is lowercase
Output 2
Enter a character: A
The character A is uppercase
Program Explanation
- The program starts by including the necessary header file
stdio.h
. - The
main()
function is defined. - Inside the
main()
function, a variablecharacter
of typechar
is declared to store the input character. - The program prompts the user to enter a character by printing the message “Enter a character: “.
- The
scanf()
function is used to read a character from the user, and the input is stored in thecharacter
variable using the%c
format specifier. A space is added before%c
inscanf
to consume any leading whitespace or newline characters from the previous input. - An if-else statement is used to check the case of the input character:
- The condition
character >= 'A' && character <= 'Z'
checks if the character is uppercase. - If the character is uppercase, it prints the message “The character [character] is uppercase.” The
%c
format specifier is used inprintf()
to print the character. - The condition
character >= 'a' && character <= 'z'
checks if the character is lowercase. - If the character is lowercase, it prints the message “The character [character] is lowercase.”
- If the character is neither uppercase nor lowercase, it prints the message “The character [character] is neither uppercase nor lowercase.”
- The condition
- The
return 0
statement ends the program.
Conclusion
This C program allows the user to enter a character and determines whether it is uppercase or lowercase using if-else statements. It covers all possible cases and provides the appropriate output based on the input character.