C++ Program to Count the Number of Spaces in a File Last Updated : 01 Aug, 2022 Comments Improve Suggest changes 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 Count the Number of Spaces in a File 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 a Line of Input Text in C++? In C++, we often need to take input from the user by reading an input text line by line but the cin method only takes input till whilespace. In this article, we will look at how to read a full line of input in C++. For Example, Input: This is a text.Output: Entered Text: This is a text.Taking a Line 2 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 C++ Program For char to int Conversion In C++, we cannot directly perform numeric operations on characters that represent numeric values. If we attempt to do so, the program will interpret the character's ASCII value instead of the numeric value it represents. We need to convert the character into an integer.Example:Input: '9'Output: 9Ex 2 min read How to Take Long String Input With Spaces in C++? In C++, we often take long strings with a lot of characters as inputs with spaces but if we use cin then our input gets terminated as soon as whitespace is encountered. In this article, we will discuss how to take long strings with spaces as input in C++. Take Long String Input With Spaces in C++To 2 min read How to Read a File Line by Line in C++? In C++, we can read the data of the file for different purposes such as processing text-based data, configuration files, or log files. In this article, we'll learn how to read a file line by line in C++. Read a File Line by Line in C++We can use the std::getline() function to read the input line by 2 min read Like