how to check for spaces in a line of words

how to check for the number os spaces in a line of words?

wad code must i use?

is it isspace()?
well there is no hardcoded way.

so just think like this. Space is also a character ' '. so you can count that
character.

1
2
3
4
5
6
7
text = "This is my line" ;
int count = 0;
for (int i = 0; i < text.size(); ++i)
if (text[i] == ' ')
++count;

// the count contains number of spaces. 
if you are using the STL the things are even easiear more
than that.

 
std::count(text.begin(), text.end(), ' ')
Pointers to a cstring can be used in place of those iterators too.
and you need to #include <algorithm> for std::count.
Also

std::count_if( text.begin(), text.end(), isspace )

if by "space" you also mean hard spaces, tabs, and/or newlines.
Topic archived. No new replies allowed.