C ctype.h Header

Character classification and conversion functions

🔤 What is ctype.h?

The ctype.h header provides functions for testing and converting characters. It includes functions to check if characters are letters, digits, or special characters, and convert between uppercase and lowercase.


#include <ctype.h>

int main() {
    char ch = 'A';
    if (isalpha(ch)) {
        printf("%c is a letter\n", ch);
        printf("Lowercase: %c\n", tolower(ch));
    }
    return 0;
}
                                    

Output:

A is a letter
Lowercase: a

Key ctype.h Functions

🔤

isalpha()

Check if character is a letter

if (isalpha('A')) { ... }
🔢

isdigit()

Check if character is a digit

if (isdigit('5')) { ... }
🔠

toupper()

Convert to uppercase

char upper = toupper('a');
🔡

tolower()

Convert to lowercase

char lower = tolower('A');

🔹 Character Classification

Character classification functions like isalpha(), isdigit(), and isspace() enable text processing and input validation in C programs. These functions from ctype.h test character properties efficiently without manual ASCII table lookup. Use isalnum() for alphanumeric validation, ispunct() for punctuation detection, and isupper() or islower() for case checking. Character classification is essential for text parsing, form validation, and command-line argument processing. Understanding these utilities significantly simplifies text processing logic.

#include <stdio.h>
#include <ctype.h>

int main() {
    char characters[] = {'A', '5', ' ', '@', 'z', '\n'};
    int size = sizeof(characters) / sizeof(characters[0]);
    
    for (int i = 0; i < size; i++) {
        char ch = characters[i];
        
        printf("Character: ");
        if (ch == ' ') printf("'space'");
        else if (ch == '\n') printf("'newline'");
        else printf("'%c'", ch);
        
        printf(" - ");
        
        if (isalpha(ch)) printf("Letter ");
        if (isdigit(ch)) printf("Digit ");
        if (isspace(ch)) printf("Whitespace ");
        if (ispunct(ch)) printf("Punctuation ");
        if (isupper(ch)) printf("Uppercase ");
        if (islower(ch)) printf("Lowercase ");
        
        printf("\n");
    }
    
    return 0;
}

Output:

Character: 'A' - Letter Uppercase 
Character: '5' - Digit 
Character: 'space' - Whitespace 
Character: '@' - Punctuation 
Character: 'z' - Letter Lowercase 
Character: 'newline' - Whitespace

🔹 Case Conversion Example

Converting between uppercase and lowercase characters simplifies text processing, string normalization, and case-insensitive comparisons in C applications. Use toupper() to convert lowercase to uppercase and tolower() for the reverse operation. These functions from ctype.h only affect alphabetic characters, leaving digits and symbols unchanged. Implement case-insensitive string comparison by converting both strings to the same case before comparison. Case conversion is fundamental for text processing, search functionality, and user input normalization.

#include <stdio.h>
#include <ctype.h>
#include <string.h>

int main() {
    char text[] = "Hello World 123!";
    char upper_text[50], lower_text[50];
    
    printf("Original: %s\n", text);
    
    // Convert to uppercase
    for (int i = 0; text[i]; i++) {
        upper_text[i] = toupper(text[i]);
    }
    upper_text[strlen(text)] = '\0';
    
    // Convert to lowercase
    for (int i = 0; text[i]; i++) {
        lower_text[i] = tolower(text[i]);
    }
    lower_text[strlen(text)] = '\0';
    
    printf("Uppercase: %s\n", upper_text);
    printf("Lowercase: %s\n", lower_text);
    
    return 0;
}

Output:

Original: Hello World 123!
Uppercase: HELLO WORLD 123!
Lowercase: hello world 123!

🧠 Test Your Knowledge

Which function checks if a character is a digit?