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

isspace() in C

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.

Similar Reads

Syntax of isspace()

isspace (character);...

How to use isspace() function in C?

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

Examples of isspace() function in C

...

Conclusion

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

FAQs on C isspace() Function

...

Contact Us