Prototype of isalpha()
This is the prototype for the function in C programming language:
Understanding isalpha()
The isalpha() function is a library function provided by “ctype.h.” This function checks whether a character is an alphabet character. If the function detects that the input character is an alphabet character (‘A’ to ‘Z’ or ‘a’ to ‘z’), it returns a non-zero integer value. But if the input character is not an alphabet character, then the function returns zero.
If you look closely at the function prototype mentioned above, the function takes one argument of the integer type. However, when we call the isaplha() function, we pass a character (‘A’ to ‘Z’ or ‘a’ to ‘z’). The value of the character is converted into an integer value. In C language, a character is stored in the memory as the corresponding ASCII value. Each alphabet has a corresponding ASCII value. For example, the ASCII value for “A” is 65, “b” is 98, etc.
Note: ASCII stands for American Standard Code for Information Interchange. The complete ASCII table can be found at the following address:
https://www.cs.cmu.edu/~pattis/15-1XX/common/handouts/ascii.html
Examples
Now that you understand the isalpha() function and its syntax, let us look at a few examples:
- Example 1: Upper-Case Alphabets
- Example 2: Lower-Case Alphabets
- Example 3: Digits
- Example 4: Special Characters
- Example 5: Practical Usage
Example 1: Upper-Case Alphabets
In this example, you will see how the isalpha() function detects upper-case alphabets.
#include <ctype.h>
int main()
{
char char_input_1 = 'A';
char char_input_2 = 'B';
char char_input_3 = 'M';
char char_input_4 = 'Y';
char char_input_5 = 'Z';
/* Check if char_input_1 is an alphabet */
if(isalpha(char_input_1))
printf("%c is an alphabet.\n",char_input_1);
else
printf("%c is not an alphabet.\n",char_input_1);
/* Check if char_input_2 is an alphabet */
if(isalpha(char_input_2))
printf("%c is an alphabet.\n",char_input_2);
else
printf("%c is not an alphabet.\n",char_input_2);
/* Check if char_input_3 is an alphabet */
if(isalpha(char_input_3))
printf("%c is an alphabet.\n",char_input_3);
else
printf("%c is not an alphabet.\n",char_input_3);
/* Check if char_input_4 is an alphabet */
if(isalpha(char_input_4))
printf("%c is an alphabet.\n",char_input_4);
else
printf("%c is not an alphabet.\n",char_input_4);
/* Check if char_input_5 is an alphabet */
if(isalpha(char_input_5))
printf("%c is an alphabet.\n",char_input_5);
else
printf("%c is not an alphabet.\n",char_input_5);
return 0;
}
Example 2: Lower-Case Alphabets
In this example, you will see how the isalpha() function detects lower-case alphabets and returns a non-zero integer value.
#include <ctype.h>
int main()
{
char char_input_1 = 'a';
char char_input_2 = 'b';
char char_input_3 = 'm';
char char_input_4 = 'y';
char char_input_5 = 'z';
/* Check if char_input_1 is an alphabet */
if(isalpha(char_input_1))
printf("%c is an alphabet.\n",char_input_1);
else
printf("%c is not an alphabet.\n",char_input_1);
/* Check if char_input_2 is an alphabet */
if(isalpha(char_input_2))
printf("%c is an alphabet.\n",char_input_2);
else
printf("%c is not an alphabet.\n",char_input_2);
/* Check if char_input_3 is an alphabet */
if(isalpha(char_input_3))
printf("%c is an alphabet.\n",char_input_3);
else
printf("%c is not an alphabet.\n",char_input_3);
/* Check if char_input_4 is an alphabet */
if(isalpha(char_input_4))
printf("%c is an alphabet.\n",char_input_4);
else
printf("%c is not an alphabet.\n",char_input_4);
/* Check if char_input_5 is an alphabet */
if(isalpha(char_input_5))
printf("%c is an alphabet.\n",char_input_5);
else
printf("%c is not an alphabet.\n",char_input_5);
return 0;
}
Example 3: Digits
In this example, you will see that the isalpha() function returns zero when we pass numerical digits.
#include <ctype.h>
int main()
{
char char_input_1 = '0';
char char_input_2 = '1';
char char_input_3 = '2';
char char_input_4 = '3';
char char_input_5 = '4';
/* Check if char_input_1 is an alphabet */
if(isalpha(char_input_1))
printf("%c is an alphabet.\n",char_input_1);
else
printf("%c is not an alphabet.\n",char_input_1);
/* Check if char_input_2 is an alphabet */
if(isalpha(char_input_2))
printf("%c is an alphabet.\n",char_input_2);
else
printf("%c is not an alphabet.\n",char_input_2);
/* Check if char_input_3 is an alphabet */
if(isalpha(char_input_3))
printf("%c is an alphabet.\n",char_input_3);
else
printf("%c is not an alphabet.\n",char_input_3);
/* Check if char_input_4 is an alphabet */
if(isalpha(char_input_4))
printf("%c is an alphabet.\n",char_input_4);
else
printf("%c is not an alphabet.\n",char_input_4);
/* Check if char_input_5 is an alphabet */
if(isalpha(char_input_5))
printf("%c is an alphabet.\n",char_input_5);
else
printf("%c is not an alphabet.\n",char_input_5);
return 0;
}
Example 4: Special Characters
In this example, you will see that the isalpha() function returns zero when we pass special characters.
#include <ctype.h>
int main()
{
char char_input_1 = '&';
char char_input_2 = '$';
char char_input_3 = '#';
char char_input_4 = '%';
char char_input_5 = '@';
/* Check if char_input_1 is an alphabet */
if(isalpha(char_input_1))
printf("%c is an alphabet.\n",char_input_1);
else
printf("%c is not an alphabet.\n",char_input_1);
/* Check if char_input_2 is an alphabet */
if(isalpha(char_input_2))
printf("%c is an alphabet.\n",char_input_2);
else
printf("%c is not an alphabet.\n",char_input_2);
/* Check if char_input_3 is an alphabet */
if(isalpha(char_input_3))
printf("%c is an alphabet.\n",char_input_3);
else
printf("%c is not an alphabet.\n",char_input_3);
/* Check if char_input_4 is an alphabet */
if(isalpha(char_input_4))
printf("%c is an alphabet.\n",char_input_4);
else
printf("%c is not an alphabet.\n",char_input_4);
/* Check if char_input_5 is an alphabet */
if(isalpha(char_input_5))
printf("%c is an alphabet.\n",char_input_5);
else
printf("%c is not an alphabet.\n",char_input_5);
return 0;
}
Example 5: Practical Usage
In this example, we will look into the practical usage of the isalpha() function in a real-world situation. Suppose that we are receiving an input character stream and we need to extract the meaningful alphabets from it. We can use the islpha() function to extract the alphabets from the input stream.
#include <ctype.h>
int main()
{
char char_input[] = "5673&^%_SOF2*!";
char char_output[10];
int i = 0, j = 0;
while(char_input[i] != '\0')
{
if(isalpha(char_input[i]))
{
char_output[j] = char_input[i];
j++;
}
i++;
}
char_output[j] = '\0';
printf("char_output = %s\n",char_output);
return 0;
}
Conclusion
In multiple examples of the practical usage of the isalpha() function, this article showed you how the isalpha() function plays a key role in detecting alphabet characters in the C programming language. This function is mainly used in embedded programming, where we receive a stream of characters and we need to extract meaningful alphabets from the input stream.