Syntax of the isalnum function
Below is the format of the isalnum method in the C programming language:
int isalnum (int ch);
Here, the isalnum function accepts 'ch' as the argument to determine whether the provided input is alphanumeric or not.
Parameters:
The 'ch' variable signifies the characters or digits that need to undergo verification.
Return Value:
It provides a value other than zero if the character 'ch' passed is alphanumeric. Otherwise, it displays 0.
Example 1: Illustration of how to utilize the isalnum function in the C programming language
Let's use an example to verify if the provided character is an alphanumeric in the C programming language.
/* get the alphanumeric number using the isalnum() function in C. */
#include <stdio.h>
#include <conio.h>
int main ()
{
// declaration of the variables
int ch1 = 'A';
int ch2 = 'e';
int ch3 = '$';
int ch4 = '7';
int ch5 = ' ';
int ch6 = '0';
/* use isalnum () function to check the given number is alphanumeric. */
if ( isalnum (ch1) )
{
printf (" '%c' is an alphanumeric character. \n", ch1);
}
else
{
printf ("'%c' is not an alphabetic or numeric character. \n", ch1);
}
if ( isalnum (ch2) )
{
printf (" '%c' is an alphanumeric character. \n", ch2);
}
else
{
printf ("'%c' is not an alphabetic or numeric character. \n", ch2);
}
if ( isalnum (ch3) )
{
printf (" '%c' is an alphanumeric character. \n", ch3);
}
else
{
printf ("'%c' is not an alphabetic or numeric character. \n", ch3);
}
if ( isalnum (ch4) )
{
printf (" '%c' is an alphanumeric character. \n", ch4);
}
else
{
printf (" '%c' is not an alphabetic or numeric character. \n", ch4);
}
if ( isalnum (ch5) )
{
printf (" '%c' is an alphanumeric character. \n", ch5);
}
else
{
printf (" '%c' is not an alphabetic or numeric character. \n", ch5);
}
if ( isalnum (ch6) )
{
printf (" '%c' is an alphanumeric character. \n", ch6);
}
else
{
printf (" '%c' is not an alphabetic or numeric character. \n", ch6);
}
return 0;
}
Output:
'A' is an alphanumeric character.
'e' is an alphanumeric character.
'$' is not an alphabetic or numeric character.
'7' is an alphanumeric character.
' ' is not an alphabetic or numeric character.
'0' is an alphanumeric character.
In the provided code snippet, the isalnum function examines the values of each variable to determine if the input character is alphanumeric. When the character is alphanumeric, it outputs that '%c' is an alphanumeric character. Otherwise, it indicates that '%c' is not an alphanumeric character.
Example 2: A script that prompts the user to enter a character and verifies its alphanumeric status by utilizing the isalnum method.
Let's take an instance where we prompt the user to enter a character and verify if the provided character is alphanumeric in the C programming language.
#include <stdio.h>
#include <ctype.h>
int main ()
{
// declare char data type variable
char ch;
printf (" \n Enter a valid character: ");
scanf (" %c", &ch);
/* use isalnum() function to check the valid alphabets (a to z or A to Z) or numeric (0 to 9) character */
if (isalnum (ch))
{
printf (" \n '%c' is a valid alphanumeric character. ", ch);
}
else
{
printf (" \n %c is not an alphanumeric character. ", ch);
printf (" \n Please, enter a valid alphanumeric character ('a' to 'z' or 'A' to 'Z' and (0 to 9)). " );
}
return 0;
}
Output:
Enter a valid character: t
't' is a valid alphanumeric character.
Here, we receive the 't' character from the user and then utilize the isalnum function to verify if the provided character is alphanumeric.
2 nd execution:
Enter a valid character: 7
'7' is a valid alphanumeric character.
Similarly, we receive the number 7 from the user and validate whether it is a numeric value or not. The isalnum method confirms that the provided number consists of digits ranging from 0 to 9, indicating it is indeed numeric.
3 rd execution:
Enter a valid character: +
'+' is not an alphanumeric character.
Please, enter a valid alphanumeric character ('a' to 'z' or 'A' to 'Z' and (0 to 9)).
Here, we provide the '+" symbol as input and utilize the isalnum function to verify if it is an alphanumeric character. The result indicates that the specified character is non-alphanumeric, meaning it is neither a letter nor a digit between 0 and 9.
A sample program in C that retrieves non-zero values for alphanumeric characters is shown below:
#include <stdio.h>
int main() {
char input;
printf("Enter a character: ");
scanf("%c", &input);
if ((input >= 'a' && input <= 'z') || (input >= 'A' && input <= 'Z') || (input >= '0' && input <= '9')) {
printf("The non-zero value for the alphanumeric character '%c' is: %d\n", input, input);
} else {
printf("The entered character is not alphanumeric.\n");
}
return 0;
}
Let's explore an illustration to verify the alphanumeric character and produce a non-zero result by utilizing the isalnum function in the C programming language.
#include <stdio.h>
#include <ctype.h>
int main ()
{
// declare character type variable
char ch;
ch = 'C';
// use isalnum() function to check uppercase alphabet
printf (" \n The result of valid uppercase alphanumeric is passed: %d ", isalnum (ch));
ch = 'h';
// use isalnum() function to check lowercase alphabet
printf (" \n The result of valid lowercase alphanumeric is passed: %d ", isalnum (ch));
ch = '05';
// use isalnum() function to check alphanumeric character
printf (" \n The result of valid alphanumeric character is passed: %d ", isalnum (ch));
ch = '91';
// use isalnum() function to check uppercase alphabet
printf (" \n The result of valid alphanumeric character is passed: %d ", isalnum (ch));
ch = '&';
// use isalnum() function to check alphanumeric character
printf (" \n The result of non-alphanumeric character is passed: %d ", isalnum (ch));
return 0;
}
Output:
The result of valid uppercase alphanumeric is passed: 1
The result of valid lowercase alphanumeric is passed: 2
The result of valid alphanumeric character is passed: 4
The result of valid alphanumeric character is passed: 4
The result of non-alphanumeric character is passed: 0
Example 4: Code snippet to verify each alphanumeric character in an array by utilizing the isalnum method.
Let's take an instance to verify if all elements in an array are alphanumeric by utilizing the isalnum function 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] = {'#','&','@','9','Z','o', '!'};
// place 0 as the return values of all character stored in str array.
int arr[7] = {0, 0, 0, 0, 0, 0, 0};
int i;
// use for loop to assign the result of the isalnum() function
for (i = 0; i < 7; i++)
{
arr[i] = isalnum (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 Alphanumeric character. ");
}
else
{
printf( " is NOT an alphabet or numeric character. ");
}
}
return 0;
}
Output:
'#' is NOT an alphabet or numeric character.
'&' is NOT an alphabet or numeric character.
'@' is NOT an alphabet or numeric character.
'9' is an Alphanumeric character.
'Z' is an Alphanumeric character.
'0' is an Alphanumeric character.
'!' is NOT an alphabet or numeric character.