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
int count = 0;
while (getline(file, line))
for (int i = 0; i < line.length(); i++)
if (line[i] == ' ' || line[i] == '\t')
std::cout << "Number of white-space characters"
<< " is " << count << "\n.";
$ g++ main.cpp
Number of white-space characters is 155.
Sanfoundry Global Education & Learning Series – 1000 C++ Programs.
If you wish to look at all C++ Programming examples, go to C++ Programs
- Get Free Certificate of Merit in C++ Programming
- Participate in C++ Programming Certification Contest
- Become a Top Ranker in C++ Programming
- Take C++ Programming Tests
- Chapterwise Practice Tests: Chapter 1,
- Chapterwise Mock Tests: Chapter 1,
, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry
. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms.
Stay connected with him at LinkedIn
Subscribe to his free Masterclasses at Youtube
& discussions at Telegram SanfoundryClasses