🎓 Top 15 Udemy Courses (80-90% Discount): My Udemy Courses - Ramesh Fadatare — All my Udemy courses are real-time and project oriented courses.
▶️ Subscribe to My YouTube Channel (178K+ subscribers): Java Guides on YouTube
▶️ For AI, ChatGPT, Web, Tech, and Generative AI, subscribe to another channel: Ramesh Fadatare on YouTube
The isalnum() function in C is a standard library function that checks if a given character is alphanumeric. It is part of the C standard library (ctype.h). This function is useful for determining if a character is a letter or a digit.
Table of Contents
- Introduction
isalnum()Function Syntax- Examples
- Checking if a Character is Alphanumeric
- Using
isalnum()with User Input
- Real-World Use Case
- Conclusion
Introduction
The isalnum() function checks if a given character is alphanumeric, meaning it is either a letter (uppercase or lowercase) or a digit. This function is useful in various scenarios, such as validating user input or parsing text.
isalnum() Function Syntax
The syntax for the isalnum() function is as follows:
#include <ctype.h>
int isalnum(int c);
Parameters:
c: The character to be checked, which is passed as anint.
Returns:
- The function returns a non-zero value (true) if the character is alphanumeric; otherwise, it returns 0 (false).
Examples
Checking if a Character is Alphanumeric
To demonstrate how to use isalnum() to check if a character is alphanumeric, we will write a simple program.
Example
#include <stdio.h>
#include <ctype.h>
int main() {
char ch = 'A';
// Check if the character is alphanumeric
if (isalnum(ch)) {
printf("'%c' is alphanumeric.\n", ch);
} else {
printf("'%c' is not alphanumeric.\n", ch);
}
return 0;
}
Output:
'A' is alphanumeric.
Using isalnum() with User Input
This example shows how to use isalnum() to check if a character provided by the user is alphanumeric.
Example
#include <stdio.h>
#include <ctype.h>
int main() {
char ch;
// Get user input for the character
printf("Enter a character: ");
scanf("%c", &ch);
// Check if the character is alphanumeric
if (isalnum(ch)) {
printf("'%c' is alphanumeric.\n", ch);
} else {
printf("'%c' is not alphanumeric.\n", ch);
}
return 0;
}
Output (example user input '7'):
Enter a character: 7
'7' is alphanumeric.
Real-World Use Case
Validating User Input
In real-world applications, the isalnum() function can be used to validate user input, ensuring that only alphanumeric characters are accepted.
Example: Validating a Username
#include <stdio.h>
#include <ctype.h>
int main() {
char username[100];
int i, is_valid = 1;
// Get user input for the username
printf("Enter a username: ");
scanf("%s", username);
// Check if the username contains only alphanumeric characters
for (i = 0; username[i] != '\0'; i++) {
if (!isalnum(username[i])) {
is_valid = 0;
break;
}
}
// Print the result
if (is_valid) {
printf("The username '%s' is valid.\n", username);
} else {
printf("The username '%s' is invalid. Only alphanumeric characters are allowed.\n", username);
}
return 0;
}
Output (example user input username "user123"):
Enter a username: user123
The username 'user123' is valid.
Conclusion
The isalnum() function is essential for checking if a character is alphanumeric in C. It is useful in various applications, particularly in fields like data validation and text processing, where it is necessary to ensure that characters belong to the alphanumeric set.
My Top and Bestseller Udemy Courses. The sale is going on with a 70 - 80% discount. The discount coupon has been added to each course below:
Build REST APIs with Spring Boot 4, Spring Security 7, and JWT
[NEW] Learn Apache Maven with IntelliJ IDEA and Java 25
ChatGPT + Generative AI + Prompt Engineering for Beginners
Spring 7 and Spring Boot 4 for Beginners (Includes 8 Projects)
Available in Udemy for Business
Building Real-Time REST APIs with Spring Boot - Blog App
Available in Udemy for Business
Building Microservices with Spring Boot and Spring Cloud
Available in Udemy for Business
Java Full-Stack Developer Course with Spring Boot and React JS
Available in Udemy for Business
Build 5 Spring Boot Projects with Java: Line-by-Line Coding
Testing Spring Boot Application with JUnit and Mockito
Available in Udemy for Business
Spring Boot Thymeleaf Real-Time Web Application - Blog App
Available in Udemy for Business
Master Spring Data JPA with Hibernate
Available in Udemy for Business
Spring Boot + Apache Kafka Course - The Practical Guide
Available in Udemy for Business
Comments
Post a Comment
Leave Comment