Finding number of spaces

okay, this is very important, here is an example:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <iostream>
#include <windows.h>
using namespace std;

int numberOfSpaces;

int getSpaceNumber(string input)
{
// need help here
return numberOfSpaces;
}

int main(void)
{
string input;
cout << "Enter a string with spaces: ";
getline(cin, input);
getSpaceNumber(input);
cout << endl << "Number of spaces: " << numberOfSpaces << endl;
system("pause");
}


Let's say I input this string: "this is a string" I want it to return 3, since there are three spaces in it. Or this: "Hello World", I want that to return 1, since there is one space.

Is there a way to do this? If so please help, thanks a lot!
Last edited on
Read all the character in the string and keep track of the spaces you find
The program that I am making needs to know how many spaces that the user inputed in there string, not for me lol....
...
That was a pointless thing to post.

I know this seems pointless but it isn't can anyone please help me?
Examine all the character in the string and keep track of the spaces you find.

If I want to examine the first character in a string I can use:

1
2
3
4
5
6
std::string str;

// ... stuff

if(str[0] == 'z') {} // this checks the 1st character in the string to see if it is a 'z'.
if(str[1] == 'z') {} // this checks the 2nd character in the string to see if it is a 'z'. 


You could use that technique to detect all the spaces ' ' in your string and add them up.
Last edited on

You need to look at every char in the input string (some type of loop would be good). Here is the start of one way to do it.

1
2
3
4
for each( char c in input )
{
  // YOUR CODE GOES HERE
}
This does not seem to work::
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#include <iostream>
using namespace std;
int getSpaceNumber(string input)
{
    int numberOfSpaces(0);


    int x = input.length();

    int c(0);
    char v = '\0';
    for(c=0;c<=(x);c++)
    {
        if (input[x]==v)
        {
            //cout<<input[x];
            numberOfSpaces++;
        }
    }
    cout<<input;
return numberOfSpaces;
}

int main(void)
{
    string input;
    cout << "Enter a string with spaces: ";
    getline(cin, input);
    cout << endl << "Number of spaces: " << getSpaceNumber(input) << endl;
    cin.get();
}



replacing '\0' by ' ' also does not work.
Last edited on
You shouldn't check the character at position x, that'll be the character after the last (the terminating \0).
And why did you look for \0 instead of spaces in the first place?
Thanks for your comments guys, here is source code I originally posted fixed:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#include <iostream>
#include <windows.h>
using namespace std;

int numberOfSpaces;

int getSpaceNumber(string input)
{
for (int i = 0; input[i] != '\0'; i++)
{
if (input[i] == ' ')
{
numberOfSpaces++;
}
}
return numberOfSpaces;
}

int main(void)
{
string input;
cout << "Enter a string with spaces: ";
getline(cin, input);
getSpaceNumber(input);
cout << endl << "Number of spaces: " << numberOfSpaces << endl;
system("pause");
}
This technically isn't allowed for strings, as they're not required to be null-terminated until you call .c_str().
1
2
const int len=input.length();
for (int i=0; i<len ; i++)

is correct.
Another way to do it:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
#include <iostream>
#include <string>
#include <algorithm>
#include <cctype>
using namespace std;

struct CharIs
{
    char ch;
    bool case_sensitive;

    CharIs(char c, bool cs=true):ch(c),
        case_sensitive(cs)
    {
        if (!case_sensitive)
            ch=tolower(ch);
    }

    bool operator()(char c)
    {
        if (!case_sensitive)
            c=tolower(c);

        return ch==c;
    }
};

int main()
{
    string str;

    cout << "enter a string:\n" << endl;
    getline(cin,str);

    cout << "\nyour string has... ";
    cout << count_if(str.begin(),str.end(),CharIs(' '));
    cout << " space(s)" << endl;

    cout << "\nyour string has... ";
    cout << count_if(str.begin(),str.end(),CharIs('A'));
    cout << " 'A'(s)" << endl;

    cout << "\nyour string has... ";
    cout << count_if(str.begin(),str.end(),CharIs('A',false));
    cout << " 'A'/'a'(s)" << endl;

    cout << "\nhit enter to quit";
    cin.get();
    return 0;
}

Another way:

1
2
3
4
5
6
7
8
9
10
11
12
int getSpaceNumber(string input)
{
  int numberOfSpaces=0;

  for each( char c in input )
  {
    if( c == ' ' )
      numberOfSpaces++;
  }

  return numberOfSpaces;
}
Last edited on
I don't know about other compilers but mine refuses to compile this... :P
I use the following in my projects:
1
2
#define foreach_e BOOST_FOREACH
#define foreach(NAME,CONTAINER)  foreach_e(auto& NAME,CONTAINER) 


Then you can write:
1
2
3
foreach(c,input)
//or
foreach_e(char c,input)


Of course some care needs to be taken with the second define. The first one is harmless, but the second one requires auto support and will conflict with identifiers named foreach if included first.
I'm using VS 2010. Lets do it with a for loop so it works for everyone.

1
2
3
4
5
6
7
8
9
10
11
12
int getSpaceNumber(string input)
{
  int numberOfSpaces=0;

  for( size_t i=0; i < input.length(); i++ )
  {
    if( input[i] == ' ' )
      numberOfSpaces++;
  }

  return numberOfSpaces;
}
Last edited on
Aah, thank you very much! :D
And then there is:

 
size_t num = std::count_if(str.begin(), str.end(), std::ptr_fun(isspace));
Topic archived. No new replies allowed.