Question on counting nonblank characters

Hey all

trying to do some c++ homework and wanting to count the number of non blank characters and have hit a bit of a wall atm :( here is what i have so far.

(also too iam getting the user to enter random data in a string so i already declared "string line;" and cin statements etc

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
int found =0;
size_t whereFound;

for (int loopBlank = 0;loopBlank < line.length();loopBlank++)
{
  string s1 = line;
  string s2;	

  whereFound = s1.find(" ");
  s2 = s1.substr (1,1);

  if (s2 == " " )
   {  
    found++ ;
   }
}


later on im wanting found to be displayed as an int in a cout statement.

any help would be much appreciated and i could be completely wrong with what i have so plz feel free to say what i have is rubbish.

thx in advance
You are making it much harder than it needs to be.

The intent of the for loop seems to be to look at each character in the string individually. That's good.

But then the body of the loop does the exact same thing every time through. Line 9 finds the first occurrence of " " in line (the first occurrence of course never changes, so it always returns the same thing). Etc.

Hint: line[ loopBlank ] returns the (loopBlank)th character in the string. All you want to do is see if this character is a space ' '.

Hey mate thx for the help so far..

i did make it less complicated and it did click about what you said with the loopBlank i got it to count the first space e.g
1 1
1 2
1 3
would return 3 spaces but if i did e.g
1 2 3 4

it would only return once space would i need a loop in there to get it to keep counting?

think its getting too late in the night for this lol
So your loop should look something like this pseudo-code:

1
2
3
for( index = 0; index < line.length(); ++index )
    if( line[ index ] == a space )
        count it!;


Are you trying to count the number of spaces in more than one line of text?

If so, then you need a second loop to basically do the above code for each line
of text.
1
2
3
4
5
6
7
8
9
10
11
#include <iostream> 
#include <cstddef> // std::size_t
#include <string> 
#include <algorithm> // std::count

std::string line("some characters and spaces. "); 
std::size_t numSpaces = 0; 
std::size_t numNonSpaces = 0; 
numSpaces = static_cast<std::size_t>(std::count(line.begin(), line.end(), ' ')); 
numNonSpaces = line.size() - numSpaces; 
std::cout << numNonSpaces << std::endl; 



You could also use std::count_if and develop a predicate function that counts characters that aren't spaces. I read somewhere that it is better to not write your own loops if there is an existing algorithm that can do the work.
Last edited on
Yes, it can be done in one line of code total using count_if and boost::lambda, but I'm guessing OP's prof won't be too happy with the solution.

1
2
std::count_if( line.begin(), line.end(), boost::lambda::_1 == ' ' ); // count spaces
std::count_if( line.begin(), line.end(), boost::lambda::_1 != ' ' ); // count non-spaces 

Topic archived. No new replies allowed.