C++ isalpha()

The isalpha() function in C++ checks if the given character is an alphabet or not.

isalpha() Prototype

int isalpha(int ch);

The isalpha() function checks if ch is an alphabetic character or not as classified by the currently installed C locale. By default, the characters are alphabets:

  • Uppercase letters: A to Z
  • Lowercase letters: a to z

The behaviour of isalpha() is undefined if the value of ch is not representable as unsigned char or is not equal to EOF.

It is defined in <cctype> header file.

isalpha() Parameters

ch: The character to check.

isalpha() Return value

The isalpha() function returns non zero value if ch is an alphabet, otherwise returns zero.

Example: How isalpha() function works

#include <cctype>
#include <iostream>
#include <cstring>

using namespace std;

int main()
{
    char str[] = "ad138kw+~!$%?';]qjj";
    int count = 0;

    for (int i=0; i<=strlen(str); i++)
    {
        if (isalpha(str[i]))
            count ++;
    }

    cout << "Number of alphabet characters:" << count << endl;
    cout << "Number of non alphabet characters:" << strlen(str)-count << endl;

    return 0;
}

When you run the program, the output will be:

Number of alphabet characters:7
Number of non alphabet characters:12