Users Online
	· Guests Online: 13
· Members Online: 0
· Total Members: 188
· Newest Member: meenachowdary055
· Members Online: 0
· Total Members: 188
· Newest Member: meenachowdary055
Forum Threads
Newest Threads
No Threads created
Hottest Threads
No Threads created
Latest Articles
Articles Hierarchy
C++ Program to Count the Number of Whitespace Characters in a File
C++ Program to Count the Number of Whitespace Characters in a File
This C++ Program which counts the number of white-space characters in the file. The program creates an input file stream, reads a line on every iteration of a while loop, increments the count variable every time a white-space character (‘ ‘) or a tab-space character (‘\t’) is encountered and the count variable is printed on the screen.
Here is source code of the C++ program which counts the number of white-space characters in a file. The C++ program is successfully compiled and run on a Linux system. The program output is also shown below.
- 
/*
- 
* C++ Program to Count the White-Spaced Characters in a File
- 
*/
- 
#include <iostream>
- 
#include <fstream>
- 
- 
int main() 
- 
{
- 
int count = 0; 
- 
std::string line; 
- 
std::ifstream file("main.cpp"); 
- 
- 
while (getline(file, line)) 
- 
{
- 
for (int i = 0; i < line.length(); i++) 
- 
{
- 
if (line[i] == ' ' || line[i] == '\t') 
- 
count++;
- 
}
- 
}
- 
std::cout << "Number of white-space characters" 
- 
<< " is " << count << "\n."; 
- 
return 0; 
- 
}
$ g++ main.cpp $ ./a.out Number of white-space characters is 155.
Comments
No Comments have been Posted.
Post Comment
Please Login to Post a Comment.
 
