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
#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:
- We include
ctype.h
to use theisalpha()
function. - We define the
isAlphabetOnly()
function, which loops through each character of the string. - Inside the loop,
isalpha()
checks whether the character is an alphabet. - If any character is not an alphabet, the function returns
0
(false). - Otherwise, after checking all characters, the function returns
1
(true). - In
main()
, we callisAlphabetOnly()
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
#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:
- We define the
isAlphabetOnly()
function, which loops through the string. - Each character is checked using ASCII comparisons.
- If a character is not within the ranges of
'A' to 'Z'
or'a' to 'z'
, the function returns0
. - If all characters are valid, the function returns
1
. - 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
#include <stdio.h>
#include <regex.h>
int isAlphabetOnly(char str[]) {
regex_t regex;
int result;
// Compile the regular expression for alphabets only
result = regcomp(®ex, "^[A-Za-z]+$", REG_EXTENDED);
if (result) {
return 0; // Compilation failed
}
// Execute regex match
result = regexec(®ex, str, 0, NULL, 0);
// Free memory allocated to regex
regfree(®ex);
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:
- We include
regex.h
to use regular expressions. - We define a pattern
"^[A-Za-z]+$"
that matches only alphabetic characters. regcomp()
compiles the regex, andregexec()
checks if the input string matches.- If
regexec()
returns0
, the string contains only alphabets. - We free allocated memory using
regfree()
.
Output:
The string contains only alphabets.