Ctype Functions: isupper() and islower()


I wouldn’t say that the isupper() and islower() functions are the opposite of the toupper() and tolower() functions, though they do play well together. This week I continue my exploration of the ctype functions.

The toupper() function converts a lowercase letter to uppercase.

The tolower() function converts an uppercase letter to lowercase.

The following code demonstrates these two functions, but also includes the islower() and isupper() functions. The result is to convert characters in the sample string into their opposite case.

2026_05_16-Lesson-a.c

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

int main()
{
    char string[] = "Nothing beats knowing the ASCII codes!\n";
    char *s;

    /* output normal */
    printf("%s",string);

    /* invert case */
    s = string;
    while( *s != '\0' )
    {
        if( islower(*s) )
            putchar( toupper(*s) );
        else if( isupper(*s) )
            putchar( tolower(*s) );
        else
            putchar(*s);
        s++;
    }

    return 0;
}

A printf() statement outputs variable string[] as-is. Then a while loop uses pointer s to process the string one character at a time.

If a character is lowercase: if( islower(*s) ) it’s converted to uppercase and output: putchar( toupper(*s) )

Likewise, when a character is uppercase: if( isupper(*s) ) it’s converted to lowercase and output: putchar( tolower(*s) )

All other characters are output as-is: putchar(*s)

Here’s a sample run:

Nothing beats knowing the ASCII codes!
nOTHING BEATS KNOWING THE ascii CODES!

As with last week’s example, it’s possible to code your own ctype functions to work with upper- and lowercase letters. The following update to the code includes my own versions of the islower(), toupper(), isupper(), and tolower() functions.

2026_05_16-Lesson-b.c

#include <stdio.h>

#define TRUE 1
#define FALSE 0

/* return TRUE for uppercase char */
int isupper(int c)
{
    if( c>='A' && c<='Z' )
        return TRUE;
    else
        return FALSE;
}

/* return TRUE for lowercase char */
int islower(int c)
{
    if( c>='a' && c<='z' )
        return TRUE;
    else
        return FALSE;
}

/* convert to uppercase */
int toupper(int c)
{
    if( islower(c) )
        c &= 0xDF;

    return(c);
}

/* convert to lowercase */
int tolower(int c)
{
    if( c>='A' && c<='Z' )
        c |= 0x20;

    return(c);
}

int main()
{
    char string[] = "Nothing beats knowing the ASCII codes!\n";
    char *s;

    /* output normal */
    printf("%s",string);

    /* invert case */
    s = string;
    while( *s != '\0' )
    {
        if( islower(*s) )
            putchar( toupper(*s) );
        else if( isupper(*s) )
            putchar( tolower(*s) );
        else
            putchar(*s);
        s++;
    }

    return 0;
}

The code define TRUE as one and FALSE as zero. These are the return values used in my isupper() and islower() functions. Each function uses an if-else construction to weed out the upper- and lowercase ASCII characters, respectively.

I also coded my own toupper() and tolower() functions. Unlike last week’s examples, this time I used logical operations to reset the fifth bit in the byte to make the conversion:

c &= 0xDF; to convert a lowercase letter to uppercase

c |= 0x20; to convert an uppercase letter to lowercase

The updated code’s output is the same as the original:

Nothing beats knowing the ASCII codes!
nOTHING BEATS KNOWING THE ascii CODES!

For this example, I’d probably combine these functions as that’s how they’re used in the code. But by splitting them, I’m able to have the functions available should they be needed individually.

For next week’s Lesson, I cover the isalnum(), isalpha(), and isdigit() ctype functions.

One thought on “Ctype Functions: isupper() and islower()

  1. I donʼt want to be too nitpicky, but why define FALSE/TRUE instead of just using stdbool.h? (Especially since the release of C23 bool is now a keyword.)

    Also there is insofar a missed opportunity insofar as you donʼt mention why they are called lowercase and uppercase letters in the first place. (For those who may never have heard of this before, the actual explanation may be surprising.)

Leave a Reply