Welcome to our comprehensive guide on the C Programming isalnum() function! In this tutorial, we'll learn about this powerful tool, understand why it's useful, and see practical examples that will help you master its use. 🎯
isalnum() Function?The isalnum() function is a built-in function in C that checks if a character is an alphabet (uppercase or lowercase) or a digit (0-9). It returns a non-zero value (true) if the character is alphanumeric, and zero (false) otherwise. 📝
The syntax for the isalnum() function is simple:
int isalnum(int c);Here, c is the character you want to check.
Let's create a program that checks if a given string contains only alphanumeric characters.
#include <stdio.h>
#include <ctype.h>
int main() {
char str[] = "Hello123";
int len = strlen(str);
for (int i = 0; i < len; i++) {
if (!isalnum(str[i])) {
printf("Invalid character found!\n");
return 1;
}
}
printf("The string contains only alphanumeric characters.\n");
return 0;
}In this example, we first include the necessary header files. We then define a string str and calculate its length. We iterate over each character in the string and check if it is alphanumeric using the isalnum() function. If any non-alphanumeric character is found, we print an error message and exit the program. Otherwise, we confirm that the string contains only alphanumeric characters. ✅
Remember to include the <ctype.h> header file to use the isalnum() function.
Which header file do you need to include to use the `isalnum()` function in C?
Next, we'll explore the isalpha() function, which checks if a character is an alphabet. Stay tuned! 🚀