Open In App

isspace() in C

Improve
Improve
Like Article
Like
Save
Share
Report

The isspace() in C is a predefined function used for string and character handling. This function is used to check if the argument contains any whitespace characters. It is declared inside <ctype.h> header file.

Syntax of isspace()

isspace (character);

Parameters of isspace()

The isspace() function takes only one parameter of type char. It is the character to be tested.

Return Value of isspace()

The isspace() function returns an integer value that tells whether the passed parameter is a whitespace character or not. The possible return values of isspace() function are:

  • If the character is a whitespace character, then the return value is non-zero.
  • If the character is not a whitespace character, then the return value is zero.

How to use isspace() function in C?

The below example demonstrates how to use the isspace() function in C language:

C




// C program to illustrate the syntax of isspace function
#include <ctype.h>
#include <stdio.h>
  
int main()
{
    // declaring a char variable to be tested
    char ch1 = ' ';
    char ch2 = 'a';
  
    // checking ch1
    if (isspace(ch1))
        printf("Entered character is space\n");
    else
        printf("Entered character %c is not space", ch1);
  
    // checking ch2
    if (isspace(ch2))
        printf("Entered character is space\n");
    else
        printf("Entered character %c is not space\n", ch2);
  
    return 0;
}


Output

Entered character is space
Entered character a is not space

The space is not the only whitespace character in the C language. There are more whitespace characters which are as follows:

Character

Name

‘ ‘

Space

‘\t’

Horizontal Tab

‘\n’

Newline

‘\v’

Vertical Tab

‘\f’

Feed

‘\r’

Carriage Return

Examples of isspace() function in C

Example 1: C Program to count the number of words in a string.

The logic of this program is that the two words in a sentence are separated by a whitespace character. So we need to count the number of whitespace characters.

C




// C Program to count the number of whitespaces in a string
#include <ctype.h>
#include <stdio.h>
  
// Function for counting spaces
int cnt_space(int i, int count, char ch)
{
  
    // input sentence
    char buf[50] = "Geeks for Geeks";
    ch = buf[0];
  
    // counting spaces
    while (ch != '\0') {
        ch = buf[i];
        if (isspace(ch)) {
            count++;
        }
        i++;
    }
  
    // returning number of spaces
    return count;
}
int main()
{
  
    char ch;
    int i = 0, count = 0;
  
    // Calling function
    count = cnt_space(i, count, ch) + 1;
  
    // printing number of spaces
    printf("Number of words in the sentence is : %d",
           count);
  
    return 0;
}


Output

Number of words in the sentence is : 3

Conclusion

The isspace() function is useful in cases when we want to process strings or characters. For example, in the word counter, we can calculate the number of words by counting whitespace and few other symbols.

FAQs on C isspace() Function

1. What is the function prototype of the isspace() function?

The function prototype of isspace() in C is:

int isspace(int _C);

2. What is the use of isspace() function in C?

It is used to check whether the given character is whitespace or not.

3. Which header file is used to import the isspace() function in C?

The isspace() function is defined inside the <ctype.h> header file. So, we need to include <ctype.h> header file to use isspace() function in our program.

4. How isspace() function works?

The isspace() function in C works by comparing the ASCII values of the character and if it matches then returns a non-zero value, otherwise zero is returned.



Last Updated : 20 Mar, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments