C ctype.h Library

C ctype.h Library

The ctype.h library in C provides functions to test and manipulate characters. It’s very useful for validating input, parsing text, and formatting strings.

All functions in ctype.h work with int values representing characters (usually char).


πŸ“Œ Common Functions in ctype.h

1️⃣ Character Testing Functions

Function Description Example
isalnum(int c) Checks if character is alphanumeric (a-z, A-Z, 0-9) isalnum('A') β†’ true
isalpha(int c) Checks if character is alphabetic isalpha('1') β†’ false
isdigit(int c) Checks if character is a digit isdigit('5') β†’ true
islower(int c) Checks if character is lowercase islower('a') β†’ true
isupper(int c) Checks if character is uppercase isupper('B') β†’ true
isspace(int c) Checks for whitespace (' ', '\t', '\n') isspace(' ') β†’ true
ispunct(int c) Checks if character is punctuation ispunct('!') β†’ true
isxdigit(int c) Checks if character is hexadecimal digit isxdigit('F') β†’ true
isprint(int c) Checks if character is printable isprint('\n') β†’ false
isgraph(int c) Checks if character has graphic representation isgraph(' ') β†’ false

2️⃣ Character Conversion Functions

Function Description Example
tolower(int c) Converts uppercase to lowercase tolower('A') β†’ 'a'
toupper(int c) Converts lowercase to uppercase toupper('b') β†’ 'B'

3️⃣ Example: Character Validation and Conversion

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

int main() {
char c;

printf("Enter a character: ");
scanf("%c", &c);

if(isalpha(c))
printf("%c is a letter.\n", c);
else if(isdigit(c))
printf("%c is a digit.\n", c);
else if(isspace(c))
printf("%c is a whitespace.\n", c);
else
printf("%c is a special character.\n", c);

printf("Uppercase: %c\n", toupper(c));
printf("Lowercase: %c\n", tolower(c));

return 0;
}

Sample Output:

Enter a character: a
a is a letter.
Uppercase: A
Lowercase: a

4️⃣ Example: Count Character Types in a String

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

int main() {
char str[] = "Hello 123!";
int letters=0, digits=0, spaces=0, punct=0;

for(int i=0; str[i]; i++) {
if(isalpha(str[i])) letters++;
else if(isdigit(str[i])) digits++;
else if(isspace(str[i])) spaces++;
else if(ispunct(str[i])) punct++;
}

printf("Letters: %d\nDigits: %d\nSpaces: %d\nPunctuation: %d\n", letters,digits,spaces,punct);

return 0;
}

Output:

Letters: 5
Digits: 3
Spaces: 2
Punctuation: 1

πŸ“Œ Summary Table

Function Purpose
isalnum Checks alphanumeric
isalpha Checks alphabetic
isdigit Checks digits
islower Checks lowercase letters
isupper Checks uppercase letters
isspace Checks whitespace characters
ispunct Checks punctuation
isxdigit Checks hexadecimal digit
isprint Checks printable characters
isgraph Checks graphic characters
tolower Converts to lowercase
toupper Converts to uppercase

βœ… Notes

  1. Functions expect int values, usually casted from char.

  2. Always include #include <ctype.h>.

  3. Useful for input validation, parsing, and formatting strings.

  4. Can be combined with string.h for advanced string processing.

CodeCapsule

Sanjit Sinha β€” Web Developer | PHP β€’ Laravel β€’ CodeIgniter β€’ MySQL β€’ Bootstrap Founder, CodeCapsule β€” Student projects & practical coding guides. Email: info@codecapsule.in β€’ Website: CodeCapsule.in

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *