Skip to Content
Course content

101: Character Classification: isalpha, isdigit, isspace

Click on the "Edit" button in the top corner of the screen to edit your slide content.

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(), and isspace() to perform the counts.
  • Print the final totals for each category.
  • Example Output: Letters: 12, Digits: 3, Spaces: 4
Rating
0 0

There are no comments for now.

to be the first to leave a comment.