C++ isalpha()

The isalpha() function in C++ checks if the given character is an alphabet or not. It is defined in the cctype header file.

Example


isalpha() Syntax

The syntax of isalpha() is:

isalpha(int ch);

Here, ch is the character that we want to check.


isalpha() Parameters

The isalpha() function takes the following parameter:

  • ch - the character to check, casted to int or EOF

isalpha() Return Value

The isalpha() function returns:

  • non-zero value if ch is an alphabet
  • zero if ch is not an alphabet

isalpha() Prototype

The prototype of isalpha() as defined in the cctype header file is:

int isalpha(int ch);

Here, ch is checked for alphabets as classified by the currently installed C locale. By default, the following characters are alphabets:

  • Uppercase Letters: 'A' to 'Z'
  • Lowercase Letters: 'a' to 'z'

isalpha() Undefined Behavior

The behaviour of isalpha() is undefined if:

  • the value of ch is not representable as unsigned char, or
  • the value of ch is not equal to EOF

Example: C++ isalpha()

Output

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

In this program, we have used a for loop and the isalpha() function to count the number of alphabets in str.

The following variables and codes are used in this program:

  • strlen(str) - gives the length of the str string
  • check - checks if str[i] is an alphabet with the help of isalpha()
  • count - stores the number of alphabets in str
  • strlen(str) - count - gives the number of non-alphabets in str