C Program to Check Uppercase Lowercase Number and Special Character
ADVERTISEMENTS
C program to check uppercase lowercase number and special characters.
In this article, you will learn how to check if the given input character is a lowercase letter or uppercase letter or a number or a special character.
To solve this we'll use conditional statements and functions, we can solve this problem in three approaches 1. if-else statement
, 2. nested if-else statement
, and 3. using functions
.
Example-1
Enter a character: k
Output :: LOWER CASE LETTER
Output :: LOWER CASE LETTER
Example-2
Enter a character: S
Output :: UPPER CASE LETTER
Output :: UPPER CASE LETTER
Example-3
Enter a character: 4
Output :: NUMBER
Output :: NUMBER
You should know about the following topics in C programming to understand this program:
- C If-else statement
- C Nested if..if-else statement
- C Functions
1 - Check Uppercase Lowercase Number and Special Characters using If-Else
// C Program to Check Uppercase Lowercase Number and Special Character using If-Else
#include <stdio.h>
// It's the main function of the program
int main() {
char char_str;
// Step-1 Take input from the user
printf("Enter a character: ");
scanf("%c", &char_str);
// Step-2 Check the character & print final output of the program
printf("Output :: ");
// Step-2.1 If the character is lowercase
if (char_str >= 'a' && char_str <= 'z') {
printf("LOWER CASE LETTER\n");
}
// Step-2.2 If the character is uppercase
else if (char_str >= 'A' && char_str <= 'Z') {
printf("UPPER CASE LETTER\n");
}
// Step-2.3 If the character is a number
else if (char_str >= '0' && char_str <= '9') {
printf("NUMBER\n");
}
// Step-2.4 If the above conditions are false then the character is a special character
else {
printf("SPECIAL CHARACTER\n");
}
return 0;
}
Output
Enter a character: k
Output :: LOWER CASE LETTER
2 - Check Uppercase Lowercase Number and Special Characters using Nested If-Else
// C Program to Check Uppercase Lowercase Number and Special Character using Nested If-Else
#include <stdio.h>
// It's the main function of the program
int main() {
char char_str;
// Step-1 Take input from the user
printf("Enter a character: ");
scanf("%c", &char_str);
// Step-2 Check the character & print final output of the program
printf("Output :: ");
// Step-2.1 If the character is lowercase
if (char_str >= 'a' && char_str <= 'z') {
printf("LOWER CASE LETTER\n");
}
else {
// Step-2.2 If the character is uppercase
if (char_str >= 'A' && char_str <= 'Z') {
printf("UPPER CASE LETTER\n");
}
else {
// Step-2.3 If the character is a number
if (char_str >= '0' && char_str <= '9') {
printf("NUMBER\n");
}
// Step-2.4 If the above conditions are false then the character is a special character
else {
printf("SPECIAL CHARACTER\n");
}
}
}
return 0;
}
Output
Enter a character: S
Output :: UPPER CASE LETTER
3 - Check Uppercase Lowercase Number and Special Characters using Function
// C Program to Check Uppercase Lowercase Number and Special Character using Function
#include <stdio.h>
// Function: To check the lowercase letter
int lower_case(char str) {
if (str >= 'a' && str <= 'z') {
printf("LOWER CASE LETTER\n");
return 1;
}
return 0;
}
// Function: To check the uppercase letter
int upper_case(char str) {
if (str >= 'A' && str <= 'Z') {
printf("UPPER CASE LETTER\n");
return 1;
}
return 0;
}
// Function: To check the number
int number(char str) {
if (str >= '0' && str <= '9') {
printf("NUMBER\n");
return 1;
}
return 0;
}
// It's the main function of the program
int main() {
char char_str;
// Step-1 Take input from the user
printf("Enter a character: ");
scanf("%c", &char_str);
// Step-2 Check the character & print final output of the program
printf("Output :: ");
// Step-3 If the character is lowercase or uppercase or number or special character
if (lower_case(char_str) || upper_case(char_str) || number(char_str)) { }
else {
printf("SPECIAL CHARACTER\n");
}
return 0;
}
Output
Enter a character: $
Output :: SPECIAL CHARACTER