Character Functions in C (ctype.h)
Is this character a letter? A digit? A space? Should it become a capital letter? Programs ask these questions all the time: when checking a password, counting words, or cleaning user input.
C answers them with small, ready-made functions in ctype.h. In this lesson you will learn the most used ones, like isalpha, isdigit, isspace, toupper and tolower, and use them to build a password checker.
What You Will Learn
- What
ctype.his. - The "is" functions that test a character.
- The "to" functions that convert a character.
- What these functions return.
- How to count letters, digits and spaces in a string.
- How to build a simple password strength checker.
Character Handling Functions in C (ctype.h)
Exam definition: Character functions are library functions declared in <ctype.h> that test a single character (such as isalpha, isdigit) or convert it (toupper, tolower). The test functions return a non-zero value for true and 0 for false.
Memory trick: Two families: is... = asks a question (yes/no). to... = does a conversion (gives a new character). "Is it a letter? Then turn it to upper case."
Syntax and List of ctype.h Functions
#include <ctype.h>
int isalpha(int c); /* returns non-zero if c is a letter, else 0 */
int toupper(int c); /* returns the capital form of c */
| Function | True (non-zero) when c is… | Example true |
|---|---|---|
isalpha(c) | A letter | 'a', 'Z' |
isdigit(c) | A digit 0–9 | '7' |
isalnum(c) | A letter or digit | 'b', '4' |
isupper(c) | A capital letter | 'Q' |
islower(c) | A small letter | 'q' |
isspace(c) | Whitespace: space, \t, \n, … | ' ' |
ispunct(c) | A punctuation/special symbol | '@', '!' |
isxdigit(c) | A hexadecimal digit | 'F', '9' |
| Function | Returns | Example |
|---|---|---|
toupper(c) | Capital form if c is a small letter, else c unchanged | toupper('g') → 'G'; toupper('5') → '5' |
tolower(c) | Small form if c is a capital letter, else c unchanged | tolower('G') → 'g' |
The "is" functions return any non-zero value for true (not always 1). So write if (isdigit(c)), not if (isdigit(c) == 1).
Example 1: Test and Convert Characters
#include <stdio.h>
#include <ctype.h>
int main(void)
{
char chars[] = {'a', 'Q', '7', ' ', '#'};
int i;
char c;
for (i = 0; i < 5; i++)
{
c = chars[i];
printf("'%c' : ", c);
if (isalpha(c))
printf("letter (%s), toggled = %c\n",
isupper(c) ? "upper" : "lower",
isupper(c) ? tolower(c) : toupper(c));
else if (isdigit(c))
printf("digit, value = %d\n", c - '0');
else if (isspace(c))
printf("whitespace\n");
else if (ispunct(c))
printf("special symbol\n");
}
return 0;
}
Output
'a' : letter (lower), toggled = A 'Q' : letter (upper), toggled = q '7' : digit, value = 7 ' ' : whitespace '#' : special symbol
Code Explanation
#include <ctype.h>gives access to all character functions.isalpha(c)checks for a letter. Inside,isupper(c)decides whether to calltolowerortoupper, which toggles the case.c - '0'turns a digit character into its number value:'7'(55) −'0'(48) = 7.isspacecatches the space;ispunctcatches'#'.toupper/tolowerreturn a new value; they don't change the variable. To change it, writec = toupper(c);.
How ctype Functions Relate to ASCII
'0' ... '9' = 48 ... 57
'A' ... 'Z' = 65 ... 90
'a' ... 'z' = 97 ... 122 small = capital + 32
isdigit(c) is like (c >= '0' && c <= '9')
isupper(c) is like (c >= 'A' && c <= 'Z')
toupper(c) is like (islower(c) ? c - 32 : c)
So why use ctype? The functions are shorter, clearer and work correctly on systems that don't use ASCII. Exams sometimes ask you to write the logic without ctype; then use the comparisons above.
Example 2: Count Character Types and Check Password Strength
#include <stdio.h>
#include <ctype.h>
void analyse(const char s[])
{
int i, upper = 0, lower = 0, digit = 0, special = 0, space = 0;
for (i = 0; s[i] != '\0'; i++)
{
if (isupper(s[i]))
upper++;
else if (islower(s[i]))
lower++;
else if (isdigit(s[i]))
digit++;
else if (isspace(s[i]))
space++;
else
special++;
}
printf("\"%s\"\n", s);
printf(" upper=%d lower=%d digit=%d special=%d space=%d\n",
upper, lower, digit, special, space);
if (i >= 8 && upper > 0 && lower > 0 && digit > 0 && special > 0 && space == 0)
printf(" Password: STRONG\n");
else
printf(" Password: WEAK\n");
}
int main(void)
{
analyse("india123");
analyse("Delhi@2025");
return 0;
}
Output
"india123" upper=0 lower=5 digit=3 special=0 space=0 Password: WEAK "Delhi@2025" upper=1 lower=4 digit=4 special=1 space=0 Password: STRONG
- Each character falls into exactly one group because of the
else ifchain. - When the loop ends,
iis the string length, soi >= 8checks the minimum length. - A strong password here needs 8+ characters with upper, lower, digit and special, and no spaces.
Related programs: count uppercase, lowercase, digit and special characters and convert uppercase to lowercase in C.
Important Points to Remember
- Include <ctype.h> for character functions.
- is... functions test; to... functions convert.
- True = any non-zero value, false = 0.
- toupper/tolower return the result; assign it to keep it.
- Non-letters pass through toupper/tolower unchanged.
- Digit character to number: c − '0'.
- They work on one character; loop to process a whole string.
Common Mistakes Beginners Make
| Mistake | What happens | Fix |
|---|---|---|
toupper(c); alone | c doesn't change | c = toupper(c); |
if (isdigit(c) == 1) | May be false even for digits | if (isdigit(c)) |
toupper(s) on a whole string | Error/wrong: works on one char | Loop: s[i] = toupper(s[i]) |
Forgetting #include <ctype.h> | Implicit declaration warning | Include the header |
Using c - 0 instead of c - '0' | Gives 55 for '7' | Subtract the character '0' |
Character Functions Practice Questions
- Which header file contains
isalphaandtoupper? - What does
toupper('9')return? - What is the difference between
isalphaandisalnum? - How do you convert the character
'5'to the integer 5? - Name three characters for which
isspacereturns true.
Show answers
<ctype.h>.'9'(unchanged; it is not a letter).isalphais true only for letters;isalnumis true for letters and digits.'5' - '0'.- Space
' ', tab'\t'and newline'\n'.
Coding Exercise
Convert a sentence to title case: the first letter of every word in capitals, the rest small. Test with "wELCOME to c PROGRAMMING".
Show solution
#include <stdio.h>
#include <ctype.h>
int main(void)
{
char s[] = "wELCOME to c PROGRAMMING";
int i, new_word = 1;
for (i = 0; s[i] != '\0'; i++)
{
if (isspace(s[i]))
{
new_word = 1;
}
else if (new_word)
{
s[i] = toupper(s[i]);
new_word = 0;
}
else
{
s[i] = tolower(s[i]);
}
}
printf("%s\n", s);
return 0;
}
Welcome To C Programming
Quick Quiz: Character Functions in C (MCQ)
-
isdigit('a')returns:A) 1 B) 0 C) 'a' D) −1
-
tolower('M')returns:A) 'M' B) 'm' C) 77 always D) 0
-
Which function checks for a letter or a digit?
A)
isalphaB)isdigitC)isalnumD)ispunct -
Which character makes
ispuncttrue?A) 'k' B) '3' C) ' ' D) '!'
-
Value of
'8' - '0':A) 8 B) 56 C) 48 D) '8'
Show answers
- B: 0 (false).
- B: 'm'.
- C:
isalnum. - D: '!'.
- A: 8.
Quick Revision for Exams and Interviews
Read this before your exam. The highlighted words are the answer.
Must-remember facts
| Header | <ctype.h> |
|---|---|
| is... functions | Test: non-zero = true, 0 = false |
| to... functions | Convert and return the new char |
| isalnum | Letter or digit |
| isspace | Space, \t, \n and more |
| Digit to number | c − '0' |
| Small vs capital | Differ by 32 in ASCII |
Frequently asked questions
- What are character handling functions in C?
- Functions in ctype.h that test (isalpha, isdigit…) or convert (toupper, tolower) a single character.
- What does isalpha return?
- A non-zero value if the character is a letter, otherwise 0.
- Does toupper change the original variable?
- No. It returns the capital letter; write
c = toupper(c).
Remember
- is = question, to = conversion.
- One character at a time: loop for strings.
Next Topic
Next lesson: Pointers in C. You will start Module 10 and learn pointers, the most powerful feature of C.