C Program to Check if String is Alphabetic
isalpha() from ctype.h and if all are alphabetic, the string is alphabetic; otherwise, it is not.Examples
How to Think About It
Algorithm
Code
#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; }
Dry Run
Let's trace the input 'Hello123' through the code
Input string
str = 'Hello123\n', i = 0, isAlpha = 1
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
Result
isAlpha = 0, print 'The string is not alphabetic.'
| Index | Character | isalpha() | isAlpha |
|---|---|---|---|
| 0 | H | true | 1 |
| 1 | e | true | 1 |
| 2 | l | true | 1 |
| 3 | l | true | 1 |
| 4 | o | true | 1 |
| 5 | 1 | false | 0 |
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
#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; }
#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; }
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.
| Approach | Time | Space | Best For |
|---|---|---|---|
| Using isalpha() | O(n) | O(1) | Readability and standard use |
| Manual ASCII check | O(n) | O(1) | Avoiding library functions |
| Recursion | O(n) | O(n) | Elegant code but uses more memory |
isalpha() from ctype.h to easily check if characters are letters.fgets() can cause incorrect results.