Explain Library Functions that Operate on Characters with Examples

Question: What are Different Functions in libc Which Operate on Characters?

Answer: C library provides two groups of functions which operate on characters and these are prototyped in header . Two groups are:

1. Character classification functions
2. Character transformation functions

Character classification functions, for example,

    int islower(int);
    int isupper(int);
    int iscntrl(int);
    int isdigit(int);
    int isxdigit(int);
    int isspace(int);
    int ispunct(int);
    int isalpha(int);
    int isalnum(int);
    int isgraph(int);
    int isprint(int);

classify the argument which is an integer containing character value. And function returns an integer, 0 or non-zero value for false and true respectively. Since standard doesn’t give a specified value, any non-zero value is considered true. For example,

    char ch1 = 'A', ch2 = '7';
    isupper(ch1);	/* checks if 'ch1' is upper case character */
    isdigit(ch2); 	/* tests if 'ch2' is a decimal digit */

Notice here that isupper(ch1) returns true if ‘ch1’ is upper case character else returns false. Similarly, isdigit(ch2) returns true if ‘ch2’ is a decimal digit else returns false. This way each classification function works.


Let’s, now, consider character transformation functions, for example,

    int toupper(int);
    int tolower(int);

Transformation functions translate upper case characters to lower case and vice versa. toupper() returns upper case equivalent of its argument and tolower() returns lower case equivalent of its argument. If argument to either function is not a character of appropriate case, then it’s returned unchanged.

For example, If in some situation, you need to test a given character, say ‘ch’, is an upper case alphabet, then write test case as,

    char ch = 'g';
    if (isupper(ch))

rather than

    if (ch >= 'A' && ch <= 'Z')

First test case is portable with any ‘Character Set’ so it executes correctly.


Let’s consider an interesting program below,

/* char_operations.c -- program manipulates characters */
#include <stdio.h>
#include <ctype.h>
int main(int argc, char *argv[])
    int i;
    char *str;
    puts("\n**Program transforms character strings on cmd-line**\n");
    for (i = 0; i < argc; i++) {
        str = argv[i];
        if (i % 2 == 0) {
            /* convert string into upper case */
            while (*str)
        else if (i % 2 == 1) {
            /* convert string into lower case */
            while (*str)
    return 0;

Output is as below:

[root@localhost ch09_StringsCharactersAndBytes]# gcc -o char_operations
[root@localhost ch09_StringsCharactersAndBytes]# ./char_operations 
"converting lower case characters to upper case" 
"and upper case characters to lower case alternatively" "thank you"
**Program transforms character strings on cmd-line**
converting lower case characters to upper case
thank you

Sanfoundry Global Education & Learning Series – 1000 C Tutorials.

If you wish to look at all C Tutorials, go to C Tutorials.

Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.