C Programming

C isspace() Function

Defined in ctype.h header file, the C isspace function tests for whitespace characters.

Basic Usage

The syntax for the isspace function is as:

int isspace(int arg);

The isspace function accepts the character value and checks if it’s a recognized C whitespace character.

The function returns a non-zero value if the character is a whitespace character and a zero if otherwise.

C isspace Function Example

The following is a simple example of the isspace function usage.

Example 1

#include <stdio.h>

#include <ctype.h>


int main() {

    int i = '\n';

    int whitspace;

    whitspace = isspace(i);

    if (whitspace == 0) {

        printf("That is not a whitespace character");

    }

    else {

        printf("Definately Whitespace character");

    }

    return 0;

}

Since \n is a valid whitespace character, the return value from the isspace function is non-zero.

Example 2

We can also use hexadecimal values to check for the specific type of whitspace character found. For example, the following implements a switch statement to check for various whitespace characters.

#include <stdio.h>

#include <ctype.h>

int main() {

    char c = ' ';

    switch(c) {

        case 0x20:

            printf("space!\n");

            break;

        case 0x09:

            printf("horizontal tab!\n");

            break;

        case 0x0a:

            printf("newline!\n");

            break;

        case 0x0b:

            printf("vertical tab!\n");

            break;

        case 0x0c:

            printf("feed!\n");

            break;

        case 0x0d:

            printf("carriage return!\n");

            break;

        default:

            printf("Done\n");

    }

    return 0;

}

An example output is as shown:

gcc -g isspace.c

./a.out

space!

Example 3

Another example implementation of the isspace function is to determine the number of spaces within a given string.

The following example code shows how to implement such logic.

#include <stdio.h>

#include <ctype.h>

int main() {

    char c;

    int i = 0;

    int count = 0;

    char str[] = "This is C programming language!";

    while (str[i] != '\0') {

        c = str[i];

        if (isspace(c)){

            count++;

        }

        i++;

    }

    printf("Whitespaces: %d \n", count);

    return 0;

}

In the example above, we start by defining a char type to store individual characters in the string. We use this character to check if it’s a whitespace value or not.

Next, we define an int i which is used to iterate over the sequences of characters in the string.

We also define a count value which stores the number of whitespaces found in the string as long as the character is not a null-terminating character.

An example output is as:

gcc -g isspace.c

./a.out

Whitespaces: 4

Valid Whitespace Characters

The following are valid C whitespace characters.

Whitespace Character Description
‘ ‘ Space
\t Horizontal tab
\n Newline
\v Vertical tab
\f Feed
\r Carriage return

Conclusion

This was a quick tutorial on various ways you can use the C isspace function.

Thank you for reading!

About the author

John Otieno

My name is John and am a fellow geek like you. I am passionate about all things computers from Hardware, Operating systems to Programming. My dream is to share my knowledge with the world and help out fellow geeks. Follow my content by subscribing to LinuxHint mailing list