check if a string contains only letters in C
Code snippet on how to check if a string contains only letters in C
#include <stdio.h>
#include <string.h>
#include <ctype.h>
int check_for_letters(char *inputString)
{
int i;
if (inputString[0] == '\0')
return 0;
for (i=0; inputString[i] != '\0'; i++)
{
if (isalpha(inputString[i]) == 0)
return 0;
}
return 1;
}
This code checks whether the string contains only letters in C. First, it includes the required libraries and a function declaration. Then, the function checks if the string is empty (returning 0) and iterates through each character with a for loop. It stops when the character is a null character (ending the string) and checks every character with the isalpha() function. If the character is not alpha character, the function immediately returns 0, otherwise it returns 1 at the end.