isalpha() Function

The isalpha() function in C checks if a character is an alphabetic letter. It is commonly used for validating input where only letters are allowed, and its behavior is influenced by the current locale settings.


Syntax of isalpha()

</>
Copy
int isalpha(int c);

Parameters

ParameterDescription
cAn integer representing the character to be checked, typically cast from a char or EOF.

Note that what is considered an alphabetic letter can depend on the locale in use. In the default “C” locale, a character is alphabetic if it would return true from either isupper() or islower(). In other locales, additional characters may be classified as alphabetic.

Return Value

The function returns a nonzero value (true) if the character is an alphabetic letter, and zero (false) otherwise.


Examples for isalpha()

Example 1: Basic Alphabetic Check

This example demonstrates the basic usage of isalpha() to check whether a single character is an alphabetic letter.

Program

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

int main() {
    int ch = 'g';

    if (isalpha(ch)) {
        printf("The character %c is an alphabetic letter.\n", ch);
    } else {
        printf("The character %c is not an alphabetic letter.\n", ch);
    }

    return 0;
}

Explanation:

  1. A character variable ch is initialized with the letter 'g'.
  2. The isalpha() function checks if 'g' is an alphabetic letter.
  3. Since 'g' is a letter, the program prints a message confirming it.

Program Output:

The character g is an alphabetic letter.

Example 2: Evaluating Non-Alphabetic Characters

This example checks a character that is not an alphabetic letter to demonstrate the function’s negative result.

Program

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

int main() {
    int ch = '9';

    if (isalpha(ch)) {
        printf("The character %c is an alphabetic letter.\n", ch);
    } else {
        printf("The character %c is not an alphabetic letter.\n", ch);
    }

    return 0;
}

Explanation:

  1. A character variable ch is set to '9'.
  2. The isalpha() function checks if '9' is an alphabetic letter.
  3. Since '9' is a digit and not a letter, the function returns false and the program prints the corresponding message.

Program Output:

The character 9 is not an alphabetic letter.

Example 3: Iterating Through a String to Identify Letters

This example demonstrates how to iterate through a string and use isalpha() to determine whether each character is an alphabetic letter.

Program

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

int main() {
    char str[] = "Hello, World!123";
    int i = 0;

    while (str[i] != '\0') {
        if (isalpha(str[i])) {
            printf("Character '%c' is an alphabetic letter.\n", str[i]);
        } else {
            printf("Character '%c' is not an alphabetic letter.\n", str[i]);
        }
        i++;
    }

    return 0;
}

Explanation:

  1. A string "Hello, World!123" is defined.
  2. The program iterates through each character in the string using a while loop.
  3. For every character, isalpha() checks if it is an alphabetic letter.
  4. A message is printed for each character indicating whether it is a letter or not.

Program Output:

Character 'H' is an alphabetic letter.
Character 'e' is an alphabetic letter.
Character 'l' is an alphabetic letter.
Character 'l' is an alphabetic letter.
Character 'o' is an alphabetic letter.
Character ',' is not an alphabetic letter.
Character ' ' is not an alphabetic letter.
Character 'W' is an alphabetic letter.
Character 'o' is an alphabetic letter.
Character 'r' is an alphabetic letter.
Character 'l' is an alphabetic letter.
Character 'd' is an alphabetic letter.
Character '!' is not an alphabetic letter.
Character '1' is not an alphabetic letter.
Character '2' is not an alphabetic letter.
Character '3' is not an alphabetic letter.