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 arguement contains any whitespace characters.
There are many types of whitespace characters in c++ such as-
- ‘ ‘ – Space
- ‘ ’ – Horizontal tab
- ‘ ’ – Newline
- ‘v’ – Vertical tab
- ‘f’ – Feed
- ‘ ’ – Carriage return
Syntax : int isspace(int x) x : x is character to be checked
Given a string, we need to count the number of whitespace characters in the string using isspace() function.
Input : string = 'Geeks for geeks' Output : 2 Input :string = 'My name is Ayush' Output : 3
Entered character is space
Application: isspace() function is used to find number of spaces in a given sentence.
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
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.