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.
Contact Us