How to count spaces in text with C++

1 Answer

0 votes
#include <iostream>
#include <fstream>

using std::cout;
using std::endl;

int main()
{
	char filename[32] = "d:\\data.txt";
	int counter = 0;
	
	std::ifstream ifs(filename);
	if (ifs) {
		char ch;
		while (ifs.get(ch))
			if (ch == ' ') counter++;
	}
	ifs.close();

	cout << counter << endl;

	return 0;
}

/*
run:

4

*/

 



answered Jun 8, 2018 by avibootz

Related questions

1 answer 215 views
1 answer 100 views
100 views asked Feb 16, 2022 by avibootz
2 answers 107 views
...