Character is a digit, alphabet or special symbol
C program to check whether given character is a digit, alphabet or special symbol.
The above problem can be programmed in three ways:
Method 1: Using predefined library functions isupper(), islower() ad isdigit().
Method 2: Using character comparison.
Method 3: Using ASCII value for comparison.
Method 1: Using predefined library functions isupper(), islower() ad isdigit().
Method 2: Using character comparison.
Method 3: Using ASCII value for comparison.
Program
#include<stdio.h>
#include<conio.h>
#include<ctype.h>
void main()
{
char ch;
printf("Enter a character\t:");
scanf("%c",&ch);
if(isupper(ch))
printf("\nCharacter entered is an UPPERCASE alphabet");
else if(islower(ch))
printf("\nCharacter entered is a lowercase alphabet");
else if(isdigit(ch))
printf("\nCharacter entered is a digit");
else
printf("\nCharacter entered is a special one");
getch();
}
Output
********** Run1 **********
Enter a character :R
Character entered is an UPPERCASE alphabet
********** Run2 **********
Enter a character :+
Character entered is a special one
Explanation
isupper() function in C language checks whether given character is in upper case (A-Z) or not.
Syntax for isupper() function is as follows.
isupper function will return an integer value as output.
islower() function checks whether a character is lowercase alphabet (a-z) or not.
Syntax for islower() function is as follows.
islower function will return an integer value as output.
isdigit() function in C language checks whether given character is digit (0-9) or not.
Syntax for isdigit() function is as follows.
isdigit function will return an integer value as output.
Even though, above functions takes integer as an argument, character is passed to the function. Internally, the character is converted to its ASCII for the check.
All these functions are defined in ctype.h header file.
Syntax for isupper() function is as follows.
int isupper(int arg);
isupper function will return an integer value as output.
If the character inside the isupper function is in uppercase, then it will return non zero value (true)
If the character is not in uppercase, it will return 0 (false) |
islower() function checks whether a character is lowercase alphabet (a-z) or not.
Syntax for islower() function is as follows.
int islower(int arg);
islower function will return an integer value as output.
If the character inside the islower function is in lowercase, then it will return non zero value (true)
If the character is not in lowercase, it will return 0 (false) |
isdigit() function in C language checks whether given character is digit (0-9) or not.
Syntax for isdigit() function is as follows.
int isdigit(int arg);
isdigit function will return an integer value as output.
If the character inside the isdigit function is a digit, then it will return non zero value (true)
If the character is not a digit, it will return 0 (false) |
Even though, above functions takes integer as an argument, character is passed to the function. Internally, the character is converted to its ASCII for the check.
All these functions are defined in ctype.h header file.
Program
#include<stdio.h>
#include<conio.h>
void main()
{
char ch;
printf("Enter a character\t:");
scanf("%c",&ch);
if(ch >= 'A' && ch <= 'Z')
printf("\nCharacter entered is in UPPERCASE");
else if(ch >= 'a' && ch <= 'z')
printf("\nCharacter entered is in lowercase");
else if(ch >= '0' && ch <= '9')
printf("\nCharacter entered is a digit");
else
printf("\nCharacter entered is a special one");
getch();
}
Output
********** Run1 **********
Enter a character :5
Character entered is a digit
********** Run2 **********
Enter a character :/
Character entered is a special one
Explanation
A character is an uppercase alphabet if it is in between A-Z.
A character is a lowercase alphabet if it is in between a-z.
A character is digit if it is in between 0-9.
A character is special symbol character if it is neither alphabet nor digit.
So, to check uppercase,
For lowercase,
For digits,
else, the character entered is a special character.
Note that, characters are written in single quotes.
Instead of characters, their ASCII value can also be used.
Note that, no single quote required when ASCII values are used.
Program for the same is in "method 3" tab.
A character is a lowercase alphabet if it is in between a-z.
A character is digit if it is in between 0-9.
A character is special symbol character if it is neither alphabet nor digit.
So, to check uppercase,
(ch >= 'A' && ch <= 'Z')
condition is used.
For lowercase,
(ch >= 'a' && ch <= 'z')
condition is used.
For digits,
(ch >= '0' && ch <= '9')
condition is used.
else, the character entered is a special character.
Note that, characters are written in single quotes.
Instead of characters, their ASCII value can also be used.
(ch >= 65 && ch <= 90)
for uppercase,
(ch >= 97 && ch <= 122)
for lowercase,
(ch >= 48 && ch <= 57)
for digit.
Note that, no single quote required when ASCII values are used.
Program for the same is in "method 3" tab.
Program
#include<stdio.h>
#include<conio.h>
void main()
{
char ch;
printf("Enter a character\t:");
scanf("%c",&ch);
if(ch >= 65 && ch <= 90)
printf("\nCharacter entered is in UPPERCASE");
else if(ch >= 97 && ch <= 122)
printf("\nCharacter entered is in lowercase");
else if(ch >= 48 && ch <= 57)
printf("\nCharacter entered is a digit");
else
printf("\nCharacter entered is a special one");
getch();
}
Output
********** Run1 **********
Enter a character :m
Character entered is in lowercase
********** Run2 **********
Enter a character :&
Character entered is a special one
Explanation
A character is an uppercase alphabet if it is in between A-Z.
A character is a lowercase alphabet if it is in between a-z.
A character is digit if it is in between 0-9.
A character is special symbol character if it is neither alphabet nor digit.
In the above program, ASCII values are used for the comparison.
So, to check uppercase,
else, the character entered is a special character.
A character is a lowercase alphabet if it is in between a-z.
A character is digit if it is in between 0-9.
A character is special symbol character if it is neither alphabet nor digit.
In the above program, ASCII values are used for the comparison.
So, to check uppercase,
(ch >= 65 && ch <= 90)
condition is used, where, 65 is the ASCII value of 'A' and 90 is the ASCII value of 'Z'.
(ch >= 97 && ch <= 122)
for lowercase, where, 97 is the ASCII value of 'a' and 122 is the ASCII value of 'z'.
(ch >= 48 && ch <= 57)
for digit, where, 48 is the ASCII value of '0' and 57 is the ASCII value of '9'.
else, the character entered is a special character.