The isalpha function in C programming checks whether the argument passed is an alphabetic character
(a to z and A-Z) or not. It is declared in ctype.h
and takes one argument.
The isalpha function tests for any character for which isupper or islower is true
, or any character that is one of a locale-specific set of alphabetic characters for which none of iscntrl, isdigit, ispunct, or isspace is true
.
Syntax of isalpha function in C:
//Syntax of isalpha int isalpha(int c);
Parameters:
c
=> character to classify
Return value:
Non-zero value
=> If the argument is an alphabetic character.
0
=> If the argument is neither an alphabet.
Note:
In the “C” locale, isalpha returns true only for the characters for which isupper or islower is true.
Example,
Input : a Output : Non-zero value Input : A Output : Non-zero value Input : @ Output : Zero
C Program to Check whether a Character Entered by User is Alphabet or not Using the isalpha:
Let’s see a C program to check given character is alphabetical or not.
#include <stdio.h> #include <ctype.h> int main() { int c; printf("Enter a Valid Character: "); c = fgetc(stdin); if (isalpha(c) == 0) { printf("%c is not an alphabetical character.", c); } else { printf("%c is an alphabetical character.", c); } return 0; }
Output1:
Enter a character: a
a is an alphabetical character.
Output2:
Enter a character: @
@ is not an alphabetical character.
There are many applications of isalpha in C programming. But to find out the number of alphabetical in a given input stream is very popular. So let’s see a C code to find the count of the alphabetical characters in the given input stream.
#include <ctype.h> #include<string.h> #include <stdio.h> unsigned int findCountAlphabetsGivenStream(char *str) { unsigned int counter = 0; if (str != NULL) { unsigned int i = 0; // counting of alphanumerics while (str[i] != '\0') { if (isalpha((unsigned char)str[i])) { ++counter; } ++i; } } // returning total number of alphanumerics // present in given input stream return (counter); } int main() { char str[] = "123_aticleworld"; unsigned int counter = findCountAlphabetsGivenStream(str); printf("Total number of char in input stream is : %u\n\n", strlen(str)); printf("\nNumber of alphabetical in the " "given input stream is : %u\n\n", counter); return 0; }
Output:
Total number of char in input stream is : 15
Number of alphabetical in the given input stream is : 11
Note:
If the argument’s value (c
) is neither representable as unsigned char not equal to EOF, the behavior of isalpha is undefined.
As we know the behavior of isalpha
is undefined if the argument’s value is neither representable as unsigned char nor equal to EOF. So to use these functions safely with plain chars (or signed chars), the argument should first be converted to unsigned char. Because it is good to convert signed char to unsigned char before being assigned or converted to a larger signed type.
int my_isalpha(char ch) { return isalpha((unsigned char)ch); }
In the “C
” locale, isalpha returns true only for the characters for which isupper or islower is true. Let’s see a C program that demonstrates the use of isalpha() with different locales (OS-specific).
#include <ctype.h> #include <stdio.h> #include <locale.h> int main() { unsigned char c = '\xDF'; // German letter ß in ISO-8859-1 printf("isalpha('\\xDF') in default C locale returned %d\n", isalpha(c)); /*If the selection cannot be honored, the setlocale function returns a null pointer and the program’s locale is not changed.*/ if (setlocale(LC_CTYPE, "de_DE.ISO8859-15") != NULL) { printf("isalpha('\\xDF') in ISO-8859-1 locale returned %d\n", isalpha(c)); } return 0; }
Output:
isalpha('\xDF') in default C locale returned 0
isalpha('\xDF') in ISO-8859-1 locale returned 1
Recommended Post:
- How to use isalnum function in C programming?
- Use of fgetc() function in C?
- How to use sizeof operator in C.
- _Alignof or alignof Operator in C
- Alignment specifiers in C ( _Alignas).
- Function Specifiers in C.
- Type qualifiers in C.
- Punctuators in C.
- Elements of C language.
- C String Literals with Its Types
- C identifiers and naming rules.
- Stringizing operator (#) in C