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
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.
Number of spaces in the sentence is : 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 contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.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.
Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: DSA Self Paced. Become industry ready at a student-friendly price.
- iscntrl() in C++ and its application to find control characters
- C Program to count the Number of Characters in a File
- unordered_multimap and its application
- C++ bitset and its application
- fesetround() and fegetround() in C++ and their application
- isupper() and islower() and their application in C++
- is_permutation() in C++ and its application for anagram search
- Synchronous Chatting Application using C++ boost::asio
- Chat application between two processes using signals and shared memory
- Full screen OpenCV / GtK application in C++ running on Raspberry PI
- Understanding ShellExecute function and it's application to open a list of URLs present in a file using C++ code
- Assigning multiple characters in an int in C language
- Find sum of integers in a file which contains any characters
- Quick way to check if all the characters of a string are same
- Remove odd frequency characters from the string
- Rearrange characters in a string such that no two adjacent are same using hashing
- Print * in place of characters for reading passwords in C
- C program to print characters without using format specifiers
- std::count() in C++ STL
- bitset count() in C++ STL
Improved By : ManasChhabra2