Fast single-character tests and conversions. All functions take an unsigned char value cast to int. Returns non-zero (true) or 0 (false).
Include: #include <ctype.h> | Compile: gcc -Wall -o prog prog.c
| Name | Prototype / Value | Description |
|---|---|---|
| isalpha | int isalpha(int c) | Non-zero if c is a letter (a-z, A-Z) |
| isdigit | int isdigit(int c) | Non-zero if c is a decimal digit (0-9) |
| isalnum | int isalnum(int c) | Non-zero if c is a letter or digit |
| isspace | int isspace(int c) | Non-zero if c is whitespace |
| isupper | int isupper(int c) | Non-zero if c is uppercase letter |
| islower | int islower(int c) | Non-zero if c is lowercase letter |
| ispunct | int ispunct(int c) | Non-zero if c is punctuation |
| isprint | int isprint(int c) | Non-zero if c is printable (includes space) |
| isgraph | int isgraph(int c) | Non-zero if c is printable and not space |
| iscntrl | int iscntrl(int c) | Non-zero if c is a control character |
| isxdigit | int isxdigit(int c) | Non-zero if c is a hex digit (0-9, a-f, A-F) |
| toupper | int toupper(int c) | Convert lowercase letter to uppercase |
| tolower | int tolower(int c) | Convert uppercase letter to lowercase |
Copy, save as demo.c, and compile with the command shown at the bottom.
#include <stdio.h>
#include <ctype.h>
void str_upper(char *s) {
for (; *s; s++) *s = (char)toupper((unsigned char)*s);
}
void analyze(const char *s) {
int letters=0, digits=0, spaces=0, other=0;
for (; *s; s++) {
unsigned char c = (unsigned char)*s;
if (isalpha(c)) letters++;
else if (isdigit(c)) digits++;
else if (isspace(c)) spaces++;
else other++;
}
printf("Letters=%d Digits=%d Spaces=%d Other=%d\n",
letters, digits, spaces, other);
}
int main(void) {
char text[] = "Hello, World! 123";
analyze(text);
str_upper(text);
printf("Upper: %s\n", text);
return 0;
}
/* Compile: gcc -Wall -o ctype_demo ctype_demo.c */