0
0
CProgramBeginner · 2 min read

C Program to Check if String is Alphabetic

Use a loop to check each character with isalpha() from ctype.h and if all are alphabetic, the string is alphabetic; otherwise, it is not.
📋

Examples

InputHelloWorld
OutputThe string is alphabetic.
InputHello123
OutputThe string is not alphabetic.
Input
OutputThe string is alphabetic.
🧠

How to Think About It

To check if a string is alphabetic, look at each character one by one. If every character is a letter (A-Z or a-z), then the string is alphabetic. If you find any character that is not a letter, stop and say the string is not alphabetic.
📐

Algorithm

1
Get the input string from the user.
2
Start from the first character and check if it is alphabetic.
3
If any character is not alphabetic, stop and return 'not alphabetic'.
4
If all characters are alphabetic, return 'alphabetic'.
💻

Code

c
#include <stdio.h>
#include <ctype.h>

int main() {
    char str[100];
    int i = 0, isAlpha = 1;

    printf("Enter a string: ");
    fgets(str, sizeof(str), stdin);

    while (str[i] != '\0' && str[i] != '\n') {
        if (!isalpha((unsigned char)str[i])) {
            isAlpha = 0;
            break;
        }
        i++;
    }

    if (isAlpha)
        printf("The string is alphabetic.\n");
    else
        printf("The string is not alphabetic.\n");

    return 0;
}
Output
Enter a string: HelloWorld The string is alphabetic.
🔍

Dry Run

Let's trace the input 'Hello123' through the code

1

Input string

str = 'Hello123\n', i = 0, isAlpha = 1

2

Check characters one by one

Check 'H' -> alphabetic, i=1 Check 'e' -> alphabetic, i=2 Check 'l' -> alphabetic, i=3 Check 'l' -> alphabetic, i=4 Check 'o' -> alphabetic, i=5 Check '1' -> not alphabetic, set isAlpha=0, break loop

3

Result

isAlpha = 0, print 'The string is not alphabetic.'

IndexCharacterisalpha()isAlpha
0Htrue1
1etrue1
2ltrue1
3ltrue1
4otrue1
51false0
💡

Why This Works

Step 1: Using isalpha()

The function isalpha() checks if a character is a letter from A-Z or a-z.

Step 2: Loop through string

We check each character until we find a non-letter or reach the end.

Step 3: Decide result

If all characters are letters, the string is alphabetic; otherwise, it is not.

🔄

Alternative Approaches

Manual ASCII check
c
#include <stdio.h>

int main() {
    char str[100];
    int i = 0, isAlpha = 1;

    printf("Enter a string: ");
    fgets(str, sizeof(str), stdin);

    while (str[i] != '\0' && str[i] != '\n') {
        if (!((str[i] >= 'A' && str[i] <= 'Z') || (str[i] >= 'a' && str[i] <= 'z'))) {
            isAlpha = 0;
            break;
        }
        i++;
    }

    if (isAlpha)
        printf("The string is alphabetic.\n");
    else
        printf("The string is not alphabetic.\n");

    return 0;
}
This method avoids using <code>ctype.h</code> but requires manual range checks.
Using recursion
c
#include <stdio.h>
#include <ctype.h>

int isAlphabetic(const char *str) {
    if (*str == '\0' || *str == '\n') return 1;
    if (!isalpha((unsigned char)*str)) return 0;
    return isAlphabetic(str + 1);
}

int main() {
    char str[100];
    printf("Enter a string: ");
    fgets(str, sizeof(str), stdin);

    if (isAlphabetic(str))
        printf("The string is alphabetic.\n");
    else
        printf("The string is not alphabetic.\n");

    return 0;
}
This recursive approach is elegant but uses more stack space for long strings.

Complexity: O(n) time, O(1) space

Time Complexity

The program checks each character once, so time grows linearly with string length.

Space Complexity

Only a few variables are used, so space is constant regardless of input size.

Which Approach is Fastest?

The loop with isalpha() is fast and simple; manual ASCII checks are similar but less readable; recursion uses more memory and is slower for long strings.

ApproachTimeSpaceBest For
Using isalpha()O(n)O(1)Readability and standard use
Manual ASCII checkO(n)O(1)Avoiding library functions
RecursionO(n)O(n)Elegant code but uses more memory
💡
Use isalpha() from ctype.h to easily check if characters are letters.
⚠️
Forgetting to handle the newline character from fgets() can cause incorrect results.