Skip to content
StudyFame

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.h is.
  • 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 */
FunctionTrue (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'
FunctionReturnsExample
toupper(c)Capital form if c is a small letter, else c unchangedtoupper('g') → 'G'; toupper('5') → '5'
tolower(c)Small form if c is a capital letter, else c unchangedtolower('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

  1. #include <ctype.h> gives access to all character functions.
  2. isalpha(c) checks for a letter. Inside, isupper(c) decides whether to call tolower or toupper, which toggles the case.
  3. c - '0' turns a digit character into its number value: '7' (55) − '0' (48) = 7.
  4. isspace catches the space; ispunct catches '#'.
  5. toupper/tolower return a new value; they don't change the variable. To change it, write c = 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 if chain.
  • When the loop ends, i is the string length, so i >= 8 checks 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

MistakeWhat happensFix
toupper(c); alonec doesn't changec = toupper(c);
if (isdigit(c) == 1)May be false even for digitsif (isdigit(c))
toupper(s) on a whole stringError/wrong: works on one charLoop: s[i] = toupper(s[i])
Forgetting #include <ctype.h>Implicit declaration warningInclude the header
Using c - 0 instead of c - '0'Gives 55 for '7'Subtract the character '0'

Character Functions Practice Questions

  1. Which header file contains isalpha and toupper?
  2. What does toupper('9') return?
  3. What is the difference between isalpha and isalnum?
  4. How do you convert the character '5' to the integer 5?
  5. Name three characters for which isspace returns true.
Show answers
  1. <ctype.h>.
  2. '9' (unchanged; it is not a letter).
  3. isalpha is true only for letters; isalnum is true for letters and digits.
  4. '5' - '0'.
  5. 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)

  1. isdigit('a') returns:

    A) 1   B) 0   C) 'a'   D) −1

  2. tolower('M') returns:

    A) 'M'   B) 'm'   C) 77 always   D) 0

  3. Which function checks for a letter or a digit?

    A) isalpha   B) isdigit   C) isalnum   D) ispunct

  4. Which character makes ispunct true?

    A) 'k'   B) '3'   C) ' '   D) '!'

  5. Value of '8' - '0':

    A) 8   B) 56   C) 48   D) '8'

Show answers
  1. B: 0 (false).
  2. B: 'm'.
  3. C: isalnum.
  4. D: '!'.
  5. 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... functionsTest: non-zero = true, 0 = false
to... functionsConvert and return the new char
isalnumLetter or digit
isspaceSpace, \t, \n and more
Digit to numberc − '0'
Small vs capitalDiffer 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.