Check if a String Contains Only Alphabets in C

To check if a string contains only alphabets in C, we need to iterate through each character and verify whether it falls within the range of uppercase (A-Z) or lowercase (a-z) letters. This can be done using functions like isalpha() from ctype.h, ASCII value comparisons, or manual checking using loops.


Examples of Checking Alphabet-Only Strings

1. Using isalpha() Function

In this example, we will use the isalpha() function from ctype.h to check if a string contains only alphabets. We will iterate through the string and validate each character.

main.c

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

int isAlphabetOnly(char str[]) {
    int i = 0;
    while (str[i] != '\0') {
        if (!isalpha(str[i])) {
            return 0; // Non-alphabet character found
        }
        i++;
    }
    return 1; // String contains only alphabets
}

int main() {
    char str[] = "HelloWorld";
    
    if (isAlphabetOnly(str)) {
        printf("The string contains only alphabets.\n");
    } else {
        printf("The string contains non-alphabet characters.\n");
    }

    return 0;
}

Explanation:

  1. We include ctype.h to use the isalpha() function.
  2. We define the isAlphabetOnly() function, which loops through each character of the string.
  3. Inside the loop, isalpha() checks whether the character is an alphabet.
  4. If any character is not an alphabet, the function returns 0 (false).
  5. Otherwise, after checking all characters, the function returns 1 (true).
  6. In main(), we call isAlphabetOnly() with a test string and print the result.

Output:

The string contains only alphabets.

2. Using ASCII Value Comparisons

Instead of using isalpha(), we can manually check if a character falls within the ASCII ranges of uppercase (65-90) or lowercase (97-122).

main.c

</>
Copy
#include <stdio.h>

int isAlphabetOnly(char str[]) {
    int i = 0;
    while (str[i] != '\0') {
        if (!((str[i] >= 'A' && str[i] <= 'Z') || (str[i] >= 'a' && str[i] <= 'z'))) {
            return 0;
        }
        i++;
    }
    return 1;
}

int main() {
    char str[] = "Test123";

    if (isAlphabetOnly(str)) {
        printf("The string contains only alphabets.\n");
    } else {
        printf("The string contains non-alphabet characters.\n");
    }

    return 0;
}

Explanation:

  1. We define the isAlphabetOnly() function, which loops through the string.
  2. Each character is checked using ASCII comparisons.
  3. If a character is not within the ranges of 'A' to 'Z' or 'a' to 'z', the function returns 0.
  4. If all characters are valid, the function returns 1.
  5. In main(), we test a string containing numbers and display the result.

Output:

The string contains non-alphabet characters.

3. Using Regular Expressions with regex.h

For advanced users, we can use regular expressions to check if a string contains only alphabets.

main.c

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

int isAlphabetOnly(char str[]) {
    regex_t regex;
    int result;

    // Compile the regular expression for alphabets only
    result = regcomp(&regex, "^[A-Za-z]+$", REG_EXTENDED);
    if (result) {
        return 0; // Compilation failed
    }

    // Execute regex match
    result = regexec(&regex, str, 0, NULL, 0);
    
    // Free memory allocated to regex
    regfree(&regex);

    return (result == 0); // Return 1 if match found, 0 otherwise
}

int main() {
    char str[] = "HelloRegex";

    if (isAlphabetOnly(str)) {
        printf("The string contains only alphabets.\n");
    } else {
        printf("The string contains non-alphabet characters.\n");
    }

    return 0;
}

Explanation:

  1. We include regex.h to use regular expressions.
  2. We define a pattern "^[A-Za-z]+$" that matches only alphabetic characters.
  3. regcomp() compiles the regex, and regexec() checks if the input string matches.
  4. If regexec() returns 0, the string contains only alphabets.
  5. We free allocated memory using regfree().

Output:

The string contains only alphabets.