Counting spaces in a string

My code is supposed to count the number of spaces in a given string but right now its counting all the letters up to the first space. I don't know if I'm using a #include <cstring> keyword anywhere thats messing it up or not. I just started learning about strings.

Also, I was wondering if there is another way to set a variable to equal a space (char space = ' ';)

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>
#include <string>
using namespace std;

int countSpaces(char input[])
{
   int num = 0;
   char space = ' ';
   char letterRead;

   for (int i = 0; input[i] > letterRead; i++)
   {
      if (letterRead = space);
      num++;
   }

   return num;
}

int main()
{
   char input[100] = "\0";

   cout << "Enter text: ";
   cin.getline(input, 100);

   int spaces = countSpaces(input);
   cout << "Number of spaces: " << spaces << endl;

   return 0;
}
Last edited on
closed account (zb0S216C)
<cstring> is old, use <string> instead. Also, in C++, use string.

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>

int main()
{
	// Char array version:
	char str[ ] = ( "a b c" );
	int iSpaces( 0 );

	for( int iLoop( 0 ); iLoop < ( sizeof( str ) / sizeof( str[ 0 ] ) ); iLoop++ )
		if( str[ iLoop ] == ' ' )
			iSpaces++;

	std::cout << "Spaces found: " << iSpaces << std::endl;

	// std::string version:
	std::string _str( "a b c" );
	int iSpaces( 0 );

	for( unsigned int iLoop( 0 ); iLoop < _str.length( ); iLoop++ )
		if( _str.at( iLoop ) == ' ' )
			iSpaces++;

	std::cout << "Spaces found: " << iSpaces << std::endl;

    std::cin.get( );
    return 0;
}


Wazzak
Last edited on
I kind of understand your code but why are there unsigned variables?
closed account (zb0S216C)
The unsigned int object matches up with the size_t( unsigned int ) which length( ) returns. Without making iLoop unsigned, the compiler should generate a warning, indicating that it's a type mismatch( Microsoft compiler ).

Wazzak
Last edited on
Ok now i understand. thanks for your help!

Edit:
I ran into another problem. The program works great when its all under one main but I need to use two functions.

After I split up the code it only finds up to two spaces and won't go any higher.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
int count( char str[] )
{
   int iSpaces = 0;

   for(unsigned int iLoop = 0; iLoop < ( sizeof( str ) / sizeof( str [0] ) ); iLoop++ )
      if(str [iLoop] == ' ' )
         iSpaces++;

   return iSpaces;
}

int main()
{
   char str[100] = "\0";

   cout << "Enter text: ";
   cin.getline( str, 100 );

   int numSpaces = count( str );

   cout << "Number of spaces: " << numSpaces << endl;

   return 0;
}


Here's the output:

Enter text: a
Number of spaces: 0

Enter text: a b
Number of spaces: 1

Enter text: a b c
Number of spaces: 2

Enter text: a b c d
Number of spaces: 2
Last edited on
closed account (S6k9GNh0)
sizeof won't return an appropriate length if I'm not mistaken. Instead, use strlen from cstring.
That sizeof statement is returning the size of the pointer, not the array (which coincidentally is 4 (32-bit pointer) / 1 (8-bit character) which is 4).
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
#include <iostream>
#include <cstring>

using namespace std;

int count( char str[] )
{
   int iSpaces = 0;

   //Forgot to mention that strlen is incredibly ineffecient... store ahead of time.
   int strSize = strlen(str);
   for(unsigned int iLoop = 0; iLoop < strSize; iLoop++ )
      if(str [iLoop] == ' ' )
         iSpaces++;

   return iSpaces;
}

int main()
{
   char str[100] = "\0";

   cout << "Enter text: ";
   cin.getline( str, 100 );

   int numSpaces = count( str );

   cout << "Number of spaces: " << numSpaces << endl;
   cin.ignore();

   return 0;
}


C-strings are far from deprecated however most C++ users won't recommend there use. I agree with this. I don't necessarily agree to use std::string but since that's in our immediate library, I can say it's safe to use std::string over c-strings. I honestly don't like the idea of a null-terminated string...

As far as why sizeof didn't work, it's because it's a compile-time directive. It also has no way of knowing whether what you passed is an array or a pointer so it assumes pointer (for various reasons). Even if it was an array, it wouldn't be able to determine the size.
Last edited on
closed account (zb0S216C)
You can go at this two ways:

1) Add another argument that takes the length of the array. Be warned though; you can easily pass on OOR index, causing issues.

2) Do what Computerquip suggested, and use strlen( ).

Wazzak
Last edited on
closed account (S6k9GNh0)
3) Or use std::string -

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 <string>

int count( const std::string& input )
{
	int iSpaces = 0;

	for (int i = 0; i < input.size(); ++i)
		if (input[i] == ' ') ++iSpaces;

	return iSpaces;
}

int main()
{
	std::string input;
	
	std::cout << "Enter text: ";
	std::getline( std::cin, input );

	int numSpaces = count( input );

	std::cout << "Number of spaces: " << numSpaces << std::endl;
	std::cin.ignore();

	return 0;
}


EDIT: You seem to be uncertain on whether or not to use C strings or std::string. There are answers to such questions but they bring more questions. As a matter of fact, I've recently started the quest on finding my preferred string library.
Last edited on
If this isn't homework, then the simplest way to count the number of spaces in a string is:

1
2
3
4
5
6
7
#include <algorithm>
#include <string>

int main() {
    std::string s = "Hello there, world!";
    std::cout << std::count( s.begin(), s.end(), ' ' ) << std::endl;
}



Thanks for the help again. Especially all the explanations about the libraries and why my for loop was doing what it did.
Topic archived. No new replies allowed.