C
Completed
-
Section 1: Getting Started
-
Section 2: Core Syntax
-
Section 3: Functions
-
Section 4: Arrays and Strings
-
Section 5: Pointers
-
Section 6: Memory Management
-
Section 7: Structures and Unions
-
Section 8: The Preprocessor and Build Process
-
Section 9: Standard Library: stdio.h
-
Section 10: Standard Library: stdlib.h
-
Section 11: Standard Library: string.h
-
Section 12: Standard Library: ctype.h and wctype.h
-
Section 13: Standard Library: math.h, complex.h, fenv.h, tgmath.h
-
Section 14: Standard Library: Type and Limit Headers
-
Section 15: Standard Library: Error Handling and Debugging
-
Section 16: Standard Library: Localization and Encoding
-
Section 17: Standard Library: time.h
-
Section 18: Standard Library: Concurrency (C11)
-
Section 19: POSIX and System Programming (unistd.h)
-
Section 20: More Data Structures
-
Section 21: Algorithms in C
-
Section 22: Bitwise Operations
-
Section 23: Command-Line Programs
-
Section 24: Debugging and Best Practices
-
Section 25: Compiler and Language Internals
-
Section 26: Embedded and Cross-Platform Considerations
-
Section 27: Networking Basics
-
Section 28: Practical Projects
-
Section 29: Interview Practice
-
Section 30: C23 Modern Features
-
Section 31: More Practice and Review
101: Character Classification: isalpha, isdigit, isspace
I was working on a small utility the other day to sanitize usernames for a database. The rule was simple: the username should only contain letters and numbers. No spaces, no punctuation, no weird symbols. My first instinct was to just check the ASCII values manually.
The manual range struggle
I started with something like this. I'll write it out here so you can see where my head was:
char c = 'A';
if (c >= 'a' && c <= 'z') {
printf("It's a letter!\n");
}
I ran this with 'A' and... nothing. Of course. I forgot that in the ASCII table, uppercase letters and lowercase letters live in two entirely different neighborhoods. To fix this, I had to expand the logic: (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z'). It works, but it's clunky. It's the kind of code that makes you squint and double-check the && and || operators three times just to be sure you didn't flip a sign. Plus, it's a pain to read.
Finding a cleaner way
I remembered there's a header file called <ctype.h> designed specifically for this. Instead of me doing the math on character ranges, C has built-in functions that return a non-zero value (true) if a character meets a certain criterion. I swapped my manual range check for isalpha().
#include <ctype.h>
char c = 'A';
if (isalpha(c)) {
printf("It's a letter!\n");
}
Now the code actually reads like an English sentence. "If is alpha, then..." Much better. But remember, our username needs to allow numbers too. I could write another range check for '0' through '9', but why bother when isdigit() exists?
if (isalpha(c) || isdigit(c)) {
printf("Character is valid for a username.\n");
} else {
printf("Invalid character detected.\n");
}
Dealing with the "invisible" characters
While testing this, I noticed something annoying. If a user accidentally hit the spacebar or entered a tab, my code would just shout "Invalid character detected!" While technically true, it's a bit blunt. I wanted to handle whitespace differently—maybe just ignore it or trim it.
I tried checking for a literal space: if (c == ' '). But then I realized a user might use a tab or a newline character. I don't want to list every single whitespace character in the ASCII table. That's where isspace() comes in. It catches spaces, tabs, vertical tabs, form feeds, and carriage returns all in one go.
Here is how the logic evolved in my final utility:
#include <stdio.h>
#include <ctype.h>
void validate_char(char c) {
if (isspace(c)) {
printf("[%c] is just whitespace. Skipping.\n", c);
} else if (isalpha(c) || isdigit(c)) {
printf("[%c] is alphanumeric. Keeping it.\n", c);
} else {
printf("[%c] is a symbol. Rejecting!\n", c);
}
}
The beauty of these functions is that they handle the messy details of the character set for you. You stop worrying about whether 'Z' is 90 or 122 and start focusing on what your program actually needs to do with the data.
📋 Practical Task
Build a String Content Analyzer
Write a program that asks the user to input a sentence. Your program should iterate through the string and count exactly how many alphabetic characters, numeric digits, and whitespace characters were used.
Requirements:
- Use
isalpha(),isdigit(), andisspace()to perform the counts. - Print the final totals for each category.
- Example Output:
Letters: 12, Digits: 3, Spaces: 4
There are no comments for now.