Note: While using the islpha function, we must add the ctype.h header file in our program.
For instance, if the provided character falls within the range of 'a' to 'z' or 'A' to 'Z', the isalpha method will yield a value other than zero. Conversely, if the character does not belong to the alphabet letters, it will return zero for characters such as '@', '$', '&', and so forth.
Input character: 'b'
Function returns 1
Input character: '&'
Function returns 0
Syntax of the isalpha function
Here is the syntax for the isalpha function in C programming:
int isalpha (char Ch);
Here, the isalpha method accepts ch as the character type parameter to verify alphabetic characters.
Parameters:
Ch : It denotes the character type variable that requires validation.
Return Value:
The isalpha function will yield a value other than zero if the character 'ch' is alphabetic; otherwise, it will return zero.
Example 1: Code to verify if a character is alphabetic by utilizing the isalpha function
Let's explore an instance to verify if a provided character is an alphabet using the isalpha function in the C language.
#include <stdio.h>
#include <ctype.h> // use ctype.h header file
int main (int argc, const char *argv [])
{
// define unsigned variable
unsigned char ch;
// assign character to the ch
ch = 'a';
/* use isalpha () function to check the alphabet character */
if (isalpha (ch) != 0)
{
printf (" \n '%c' is a valid alphabetic character. ", ch);
}
else
{
printf (" '%c' is not the valid alphabetic character. ", ch);
}
// assign a non alphabetic character to the ch variable
ch = '&';
/* use isalpha () function to check the alphabet character */
if (isalpha (ch) != 0)
{
printf (" \n '%c' is a valid alphabetic character. ", ch);
}
else
{
printf (" \n '%c' is not a valid alphabetic character. ", ch);
}
return 0;
}
Output:
'a' is a valid alphabetic character.
'&' is not a valid alphabetic character.
Example 2: Code to authenticate the specified character utilizing the isalpha method
Let's explore an example where we utilize the isalpha function to verify if different characters are alphabetic in C programming.
#include <stdio.h>
#include <ctype.h> // use ctype.h header file
int main ()
{
// check character 'e'
/* use isalpha () function to check the alphabet character */
if (isalpha ( 'e') == 0)
{
printf (" \n 'e' is not an alphabetic character. ");
}
else
{
printf ("\n 'e' is an alphabetic character. ");
}
// check character 'Z'
if (isalpha ('Z') == 0)
{
printf (" \n 'Z' is not an alphabetic character. ");
}
else
{
printf (" \n 'Z' is an alphabetic character. ");
}
// check character '@'
if (isalpha ('@') == 0)
{
printf (" \n '@' is not an alphabetic character. ");
}
else
{
printf (" \n '@' is an alphabetic character. ");
}
// check character '5'
if (isalpha ('5') == 0)
{
printf (" \n '5' is not an alphabetic character. ");
}
else
{
printf (" \n '5' is an alphabetic character. ");
}
return 0;
}
Output:
'e' is an alphabetic character.
'Z' is an alphabetic character.
'@' is not an alphabetic character.
'5' is not an alphabetic character.
A sample code snippet to prompt the user for a character input and verify if the entered character is considered valid.
Let's explore an illustration to verify acceptable characters utilizing the isalpha function in the C programming language.
#include <stdio.h>
#include <ctype.h>
int main ()
{
// declare character type variable
char ch;
printf (" \n Enter a valid character: ");
scanf (" %c", &ch);
// use isalpha() function to check the valid character
if (isalpha (ch))
{
printf (" \n You entered a valid alphabet. ");
}
else
{
printf (" \n %c is not an alphabet. ");
printf (" \n Please, enter a valid alphabet character ('a' to 'z' or 'A' to 'Z'). " );
}
return 0;
}
Output:
Enter a valid character: g
You entered a valid alphabet.
In the program mentioned previously, we prompt the user to input the character 'g' and subsequently employ the isalpha method to verify if the entered character is indeed an alphabetic character. When the character is confirmed to be an alphabet, the program displays the message "You have input a valid character".
2 nd execution:
Enter a valid character: $
$ is not an alphabet.
Please, enter a valid alphabet character ('a' to 'z' or 'A' to 'Z').
In the subsequent run, we provide the input of the '$' symbol and employ the isalpha method to verify if the character is an alphabetic character. As the '$' symbol is not an alphabet, the function outputs "$ is not an alphabetic character. Kindly input a valid alphabet character ('a' to 'z' or 'A' to 'Z')".
Example 4: Code snippet that retrieves non-zero values for alphabetic characters by utilizing the isalpha function
Let's explore an instance where we verify the presence of alphabetic characters and receive a non-zero output by employing the isalpha function within the C programming language.
#include <stdio.h>
#include <ctype.h>
int main ()
{
// declare character type
char ch;
ch = 'C';
// use isalpha() function to check uppercase alphabet
printf (" \n The result of valid uppercase alphabetic character is passed: %d ", isalpha (ch));
ch = 'h';
// use isalpha() function to check lowercase alphabet
printf (" \n The result of valid lowercase alphabetic character is passed: %d ", isalpha (ch));
ch = '+';
// use isalpha() function to check non-alphabetic character
printf (" \n The result of non-alphabetic character is passed: %d ", isalpha (ch));
return 0;
}
Output:
The result of valid uppercase alphabetic character is passed: 1
The result of valid lowercase alphabetic character is passed: 2
The result of non-alphabetic character is passed: 0
In the previous code snippet, distinct non-zero values are obtained upon passing alphabetic characters to the isalpha function. Conversely, the function consistently yields 0 when non-alphabetic characters are provided as input.
Example 5: Script to validate all legitimate characters within the array by utilizing the isalpha method.
Let's generate an illustration showcasing the utilization of the isalpha function to verify the character array in the C programming language.
#include<stdio.h>
#include<stdlib.h>
#include <ctype.h>
int main()
{
// declaration of the character type array
char str[7] = {'*','$','@','7','Z','p', '!'};
// insert 0 as the return values to all character type array in str .
int arr[7] = {0, 0, 0, 0, 0, 0, 0};
int i;
// use for loop to assign the result of the isalpha() function
for (i = 0; i < 7; i++)
{
arr[i] = isalpha (str[i]);
}
for (i = 0; i < 7; i++)
{
printf( " \n '%c'", str[i]); // print character
// check array element is not equal to 0
if (arr[i] != 0)
{
printf( " is an Alphabetic character. ");
}
else
{
printf( " is NOT an Alphabetic character. ");
}
}
return 0;
}
Output:
'*' is NOT an Alphabetic character.
'$' is NOT an Alphabetic character.
'@' is NOT an Alphabetic character.
'7' is NOT an Alphabetic character.
'Z' is an Alphabetic character.
'p' is an Alphabetic character.
'!' is NOT an Alphabetic character.