isspace() in C/C++ and its application to count whitespace characters

15 Jun, 2022
isspace() function In C++, isspace is a predefined function used for string and character handling.cstring is the header file required for string functions and cctype is the headerfile required for character functions. This function is used to check if the argument contains any whitespace characters. There are many types of whitespace characters in c++ such as-
‘ ‘ – Space
‘\t’ – Horizontal tab
‘\n’ – Newline
‘\v’ – Vertical tab
‘\f’ – Feed
‘\r’ – Carriage return
Syntax :
int isspace(int x)
x : x is character to be checked
Application Given a string, we need to count the number of whitespace characters in the string using isspace() function. Examples:
Input : string = 'Geeks for geeks'
Output : 2

Input :string = 'My name is Ayush'
Output : 3
C++
C
// C++ program to illustrate// isspace() function #include <iostream>using namespace std; int main(){    // taking input    char ch = ' ';     // checking is it space?    if (isspace(ch))        cout << "\nEntered character is space";    else        cout << "\nEntered character is not space";       return 0;} // This code is contributed by sarajadhav12052009
// C program to illustrate// isspace() function#include <ctype.h>#include <stdio.h>int main(){    // taking input    char ch = ' ';     // checking is it space?    if (isspace(ch))        printf("\nEntered character is space");    else        printf("\nEntered character is not space");}
Output :
Entered character is space
Application: isspace() function is used to find number of spaces in a given sentence. Examples:
Input : This is a good website
Output : Number of spaces in the sentence is : 4

Input : heyy this is geeksforgeeks
Output : Number of spaces in the sentence is : 3

Input : hello how can I help you
Output : Number of spaces in the sentence is : 5
Algorithm 1. Traverse the given string character by character upto its length, check if character is a whitespace character. 2. If it is a whitespace character, increment the counter by 1, else traverse to the next character. 3. Print the value of the counter. 
C++
C
// C++ program to illustrate// isspace() function #include <iostream>using namespace std; // Function for counting spacesint 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);     // printing number of spaces    cout << "\nNumber of spaces in the sentence is : " << count;     return 0;} // This code is contributed by sarajadhav12052009
// C program to illustrate// isspace() function#include <ctype.h>#include <stdio.h> // Function for counting spacesint 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);     // printing number of spaces    printf("\nNumber of spaces in the sentence is : %d", count);     return 0;}
Output:
Number of spaces in the sentence is : 2
CPP
// CPP program to count white spaces in a string#include <iostream>#include <cstring>#include <cctype>using namespace std; // function to calculate whitespacesvoid space(string& str){    int count = 0;    int length = str.length();    for (int i = 0; i < length; i++) {        int c = str[i];        if (isspace(c))            count++;    }    cout << count;} // Driver Codeint main(){    string str = "Geeks for geeks";    space(str);    return 0;}
Output:
2
This article is contributed by Ayush Saxena and Kanchan Ray. If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks. Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
ManasChhabra2
sarajadhav12052009
C-String
CPP-Library
cpp-string
C Language
C++
CPP
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Substring in C++
Multidimensional Arrays in C / C++
Function Pointer in C
Left Shift and Right Shift Operators in C/C++
Different Methods to Reverse a String in C++
Vector in C++ STL
Map in C++ Standard Template Library (STL)
Initialize a vector in C++ (7 different ways)
Set in C++ Standard Template Library (STL)
Priority Queue in C++ Standard Template Library (STL)