C Program to Check Whether a Character is Uppercase or Lowercase or a Digit Using inbuilt ctype.h library functions
This C program checks whether a given character is uppercase, lowercase or a digit using inbuilt function
isdigit()
,
isalpha()
,
isupper()
and
islower()
.
/* Aim: To Check Whether given character is digit OR lowercase or uppercase alphabet using inbuilt library functions */
#include
#include
int main()
{
char a;
printf("\n \n Enter your character:");
scanf("%c",&a);
if (isdigit(a))
{
printf("\n \n The Character %c is digit \n \n",a);
}
else if (isalpha(a))
{
if(islower(a))
printf("\n \n The Character %c is lowercase alphabet \n \n ",a);
else if(isupper(a))
printf("\n \n The character %c is uppercase aplhabet \n \n ",a);
}
else{
printf("\n \n The character %c you entered is neither uppercase/lowercase alphabet nor a digit \n \n",a);
}
return 0;
}
Output:
Test Case 1:
Enter your character:1
The Character 1 is digit
Test Case 2:
Enter your character:a
The Character a is lowercase alphabet
Test Case 3:
Enter your character:B
The character B is uppercase aplhabet
Test Case 4:
Enter your character:#
The character # you entered is neither uppercase/lowercase alphabet nor a digit
Explanation:
ctype.h
library in C provides character related function like
isalpha()
,
isupper()
,
islower()
,
ispunct()
,
isalnum()
,
isspace()
,
toupper()
,
to lower()
.
isalpha()
function can be used to check whether given character is a digit or not.
isalpha()
is an inbuilt ctype library function which return a value grater than 0 if the character passed to it is alphabet, otherwise return 0.
isupper()
function is used to check whether given character is in uppercase.
islower()
function is used to check whether given character is in lowercase.
isdigit()
function function is used to check whether given character is a digit or not. The return value for these three functions are same as that for
isalpha()
function.
Check Character is Uppercase or Lowercase or a Digit Using ASCII values
This C program checks whether the given character is uppercase, lowercase or a digit using ASCII values.
/* Aim: To Check Whether given character is digit or lowercase or uppercase alphabet */
#include<stdio.h>
int main()
{
char a;
printf("\n \n Enter your character:");
scanf("%c",&a);
if ((a>=48) && (a<=57))
{
printf("\n \n The Character %c is digit \n \n",a);
}
else if ((a>=97) && (a<=122))
{
printf("\n \n The Character %c is lowercase alphabet \n \n ",a);
}
else if ((a>=65) && (a<=90))
{
printf("\n \n The character %c is uppercase aplhabet \n \n ",a);
}
else{
printf("\n \n The character %c you entered is neither uppercase/lowercase alphabet nor a digit \n \n",a);
}
return 0;
}
Explanation:
The ascii values for digits 0-9 range from 48-57. 48 for 0, 49 for 1, 50 for 2 and so on upto 57 for 9. The ascii values for uppercase alphabets range from 65 to 90. 65 for 'A', 66 for 'B', 67 for 'C' and so on uptp 90 for 'Z'. The ascii values for lowercase alphabets range from 97 to 122. 97 for 'a', 98 for 'b', 99 for 'c' and so on upto 122 for 'z'. We just checked the ascii of input character with these ranges using control statement
if-else.