C++ Program to Count the Number of Spaces in a File Last Updated : 01 Aug, 2022 Summarize Comments Improve Suggest changes Share Like Article Like Report Here, we will see how to count number of spaces in a given file. First, we will read the contents of the file word by word, keep one counter variable 'count', and set it to zero while declaring. Increment 'count' each time you read a single word from the file. Example: Input: Geeks For Geeks Output: There are 2 whitespaces in file Approach:Open the file which contains a string. For example, a file named "file.txt" contains the string "Geeks For Geeks".Create a string variable to store the string extracted from the file.Create one counter variable to count the number of whitespaces in a file.Display the number of total whitespaces in a file. C++ // C++ program to demonstrate the // number of whitespaces in a // file #include <bits/stdc++.h> using namespace std; int main() { // filestream variable fstream f1; // string variable string ch; // counter variable int count = 0; // opening file for reading contents f1.open("file14.txt", ios::in); while (!f1.eof()) { // extracting words from file f1 >> ch; // incrementing counter variable count++; } f1.close(); // displaying total number of whitespaces in a file cout << "There are"<<--count<<" whitespaces in file"; return 0; } Output: There are 2 whitespaces in fileNote: We have decrementing counter variable while displaying because the C++ also reads the newline comes at the end of the file. We want an only the number of whitespaces, not newlines. Comment More infoAdvertise with us Next Article C++ Program to Print the First Letter of Each Word of a String S suyashdashputre Follow Improve Article Tags : C++ Programs C++ C++ File Programs Practice Tags : CPP Similar Reads C++ Program to Split a String Into a Number of Sub-Strings Splitting a string by some delimiter is a very common task. For example, we have a comma-separated list of items from a file and we want individual items in an array. Almost all programming languages, provide a function split a string by some delimiter. In C++Note: The main disadvantage of strtok() 3 min read How to Read a Paragraph of Text with Spaces in C++? In C++, we may need to read the input that includes the paragraph of text with spaces. In this article, we will learn how to read a paragraph of text with spaces in C++. Reading a Paragraph in C++To read a paragraph of text that includes spaces, we can use the std::getline() function that can read t 2 min read C++ Program to Print the First Letter of Each Word of a String String str is given which contains lowercase English letters and spaces. It may contain multiple spaces. Get the first letter of every word and return the result as a string. The result should not contain any space. Examples: Input: str = "geeks for geeks" Output: gfg Input: str = "happy coding" Out 4 min read How to Read Multiple Numbers from a Single Line of Input in C++? In C++, when working with user inputs we often need to take input of multiple numbers from a user in a single line. In this article, we will learn how to read multiple numbers from a single line of input in C++. Example Input: 1 7 0 4 6 8 Output: Entered Number: 1, 7, 0, 4, 6, 8Take Multiple Numbers 2 min read C Program to Extract Characters From a String Character extraction can be done by iterating through the string in the form of a character array. It basically means plucking out a certain amount of characters from an array or a string. Now, to take input in C we do it by using the following methods: scanf("%c",&str[i]); - Using a loopscanf(" 2 min read How to Calculate the Size of a Static Array in C++? In C++, static arrays are the type of arrays whose size is fixed, and memory for them is allocated during the compile time. In this article, we will learn how to calculate the size of a static array in C++. Example: Input:myArray = {1, 2, 3, 4, 5}Output:The size of the array is 5.Size of a Static Ar 2 min read Like