C++ check if a character is alphabetic using isalpha

C++ isalpha method:

isalpha is a method defined in the cctype header. This method is used to check if a character is alphabetic letter or not. It depends on the locale of the system.

In this post, we will learn how to use isalpha method with examples.

Definition of isalpha:

isalpha is defined as below:

int isalpha(int c)

Here,

  • c is the character to check, which is casted to int, or EOF.

It returns one integer value. 0 for false and other non-zero value for true.

Example of isalpha:

Let’s take an example on isalpha:

#include <iostream>
#include <cctype>

using namespace std;

int main()
{
    char ch;
    
    cout<<"Enter a character : "<<endl;
    cin>>ch;

    if(isalpha(ch)){
        cout<<"It is an alphabetic character !"<<endl;
    }else{
        cout<<"It is not an alphabetic character !"<<endl;
    }
}

Here,

  • This program takes one character as input from the user and storing it in ch.
  • Using isalpha, it is checking if the character is alphabetic character or not and based on the result, it is printing one message.

It will print output as like below:

Enter a character : 
a
It is an alphabetic character !

Enter a character : 
1
It is not an alphabetic character !