• Register
100 points
7 4

Char is the data type in which can store character types like alphabets. But we can also store numeric values and many more things. You may think what’s the difference then, the difference is only the data type of the variable. In this article we will talk about how to check if a char is a letter i.e., alphabet and create a little program to find out.

Approach

We have isdigit() function to find out whether the input is numeric or not. Similarly C++ also provides isalpha() function to find out whether the character is letter (alphabet) or not. This function is defined in the “ctype.h” header file.

  1. Add “ctype.h” header file
  2. Get or add your char element
  3. Create an if statement with isalpha() function as parameter
  4. When we call isalpha() function it will let us know

Program

#include<stdio.h>
#include<ctype.h>

int main() {
   char val1 = 'k';
   char val2 = '3';

   if(isalpha(val1))
   printf("The character is an alphabet\n");
   else
   printf("The character is not an alphabet\n");

   if(isalpha(val2))
   printf("The character is an alphabet\n");
   else
   printf("The character is not an alphabet");

   return 0;
}

You can also use cout operator to print output. Like this

#include<stdio.h>
#include<ctype.h>

int main() {
   char val1 = 'k';
   char val2 = '3';

   if(isalpha(val1))
   {cout << "character is an alphabet\n";
} else { cout << "The character is not an alphabet\n";
}

   
   if(isalpha(val2))
   {cout << "character is an alphabet\n";
} else { cout << "The character is not an alphabet\n";
}

   return 0;
}

 

Output

The character is an alphabet
The character is not an alphabet
 
100 points
7 4