Chapter 30Beginner
C ctype.h Library Reference
Complete reference for ctype.h - character classification (isalpha, isdigit) and conversion (toupper, tolower).
12 min readUpdated 2024-12-16
ctype.hisalphaisdigitisspacetouppertolower
What You Will Learn
- ✓Check character types
- ✓Convert character case
- ✓Validate user input
01Introduction to ctype.h
🔤 What is ctype.h?
<ctype.h> provides functions to classify and convert characters. Perfect for input validation and text processing!
| Category | Functions |
|---|---|
| Classification | isalpha, isdigit, isalnum, isspace, ispunct |
| Case Check | isupper, islower |
| Conversion | toupper, tolower |
| Other | isprint, isgraph, iscntrl, isxdigit |
02Character Classification
| Function | Prototype | Returns True If |
|---|---|---|
| isalpha() | int isalpha(int c) | Letter (A-Z, a-z) |
| isdigit() | int isdigit(int c) | Digit (0-9) |
| isalnum() | int isalnum(int c) | Letter OR digit |
| isspace() | int isspace(int c) | Whitespace (space, tab, newline) |
| ispunct() | int ispunct(int c) | Punctuation (!@#$...) |
| isxdigit() | int isxdigit(int c) | Hex digit (0-9, A-F, a-f) |
| isprint() | int isprint(int c) | Printable (including space) |
| isgraph() | int isgraph(int c) | Printable (excluding space) |
| iscntrl() | int iscntrl(int c) | Control character |
classification.c
C
1#include <stdio.h>2#include <ctype.h>34int main() {5 char ch = 'A';6 7 // Test single character8 printf("'%c' isalpha: %d\n", ch, isalpha(ch)); // 1 (true)9 printf("'%c' isdigit: %d\n", ch, isdigit(ch)); // 0 (false)10 printf("'%c' isalnum: %d\n", ch, isalnum(ch)); // 1 (true)11 12 // Test different characters13 printf("'5' isdigit: %d\n", isdigit('5')); // 114 printf("' ' isspace: %d\n", isspace(' ')); // 115 printf("'!' ispunct: %d\n", ispunct('!')); // 116 printf("'F' isxdigit: %d\n", isxdigit('F')); // 117 18 return 0;19}💡 Return Values
These functions return non-zero (true) or 0 (false). Use them in conditions directly!
03Case Check and Conversion
| Function | Prototype | Description |
|---|---|---|
| isupper() | int isupper(int c) | Check if uppercase (A-Z) |
| islower() | int islower(int c) | Check if lowercase (a-z) |
| toupper() | int toupper(int c) | Convert to uppercase |
| tolower() | int tolower(int c) | Convert to lowercase |
case_functions.c
C
1#include <stdio.h>2#include <ctype.h>34int main() {5 char ch = 'a';6 7 // Case checking8 printf("'%c' isupper: %d\n", ch, isupper(ch)); // 09 printf("'%c' islower: %d\n", ch, islower(ch)); // 110 11 // Case conversion12 printf("toupper('a') = '%c'\n", toupper('a')); // 'A'13 printf("tolower('Z') = '%c'\n", tolower('Z')); // 'z'14 15 // Non-letters are unchanged16 printf("toupper('5') = '%c'\n", toupper('5')); // '5'17 printf("tolower('!') = '%c'\n", tolower('!')); // '!'18 19 return 0;20}04Practical Examples
Convert String to Uppercase
main.c
C
void toUpperStr(char *str) { while (*str) { *str = toupper(*str); str++; }}int main() { char text[] = "Hello World!"; toUpperStr(text); printf("%s\n", text); // HELLO WORLD! return 0;}Count Letters and Digits
main.c
C
void countChars(const char *str) { int letters = 0, digits = 0, spaces = 0; while (*str) { if (isalpha(*str)) letters++; else if (isdigit(*str)) digits++; else if (isspace(*str)) spaces++; str++; } printf("Letters: %d, Digits: %d, Spaces: %d\n", letters, digits, spaces);}int main() { countChars("Hello 123 World"); // Output: Letters: 10, Digits: 3, Spaces: 2 return 0;}Validate Alphanumeric Username
main.c
C
int isValidUsername(const char *username) { if (!*username) return 0; // Empty string // First char must be letter if (!isalpha(*username)) return 0; // Rest must be alphanumeric while (*username) { if (!isalnum(*username)) return 0; username++; } return 1;}int main() { printf("user123: %s\n", isValidUsername("user123") ? "Valid" : "Invalid"); printf("123user: %s\n", isValidUsername("123user") ? "Valid" : "Invalid"); printf("user@123: %s\n", isValidUsername("user@123") ? "Valid" : "Invalid"); // user123: Valid // 123user: Invalid // user@123: Invalid return 0;}Capitalize First Letter of Each Word
main.c
C
void capitalizeWords(char *str) { int newWord = 1; // Start with new word while (*str) { if (isspace(*str)) { newWord = 1; } else if (newWord) { *str = toupper(*str); newWord = 0; } str++; }}int main() { char text[] = "hello world from c"; capitalizeWords(text); printf("%s\n", text); // Hello World From C return 0;}05Character Classification Chart
| Char | alpha | digit | alnum | space | punct | upper | lower |
|---|---|---|---|---|---|---|---|
| 'A' | ✓ | ✗ | ✓ | ✗ | ✗ | ✓ | ✗ |
| 'a' | ✓ | ✗ | ✓ | ✗ | ✗ | ✗ | ✓ |
| '5' | ✗ | ✓ | ✓ | ✗ | ✗ | ✗ | ✗ |
| ' ' | ✗ | ✗ | ✗ | ✓ | ✗ | ✗ | ✗ |
| '!' | ✗ | ✗ | ✗ | ✗ | ✓ | ✗ | ✗ |
06Summary
🎯 Key Functions
Classification:
isalpha, isdigit, isalnum, isspace, ispunct
Case Check:
isupper, islower
Conversion:
toupper, tolower
Other:
isprint, isxdigit, iscntrl
08Next Steps
Learn about date and time functions: