Counting number of spaces in a given sentence with a function?

I'm at a loss at how to go about this. I know I have to have a for loop, then use ++ as necessary, but I'm stuck. Here's what I have so far:

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>

using namespace std;

void countChars();

int main()
{
	int numspaces;
	countChars(numspaces);
	cout << "The number of spaces is " << endl;

	return 0;
}

void countChars(int spaces, int totalchars, string input)
{

	spaces = 0;
	cout << "Enter a sentence: " << endl;
	cin.getline >> input;

	totalchars = 0;

	for (totalchars = 0, )
}
feel free to ask questions if you have an issue.
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


/*
 " Counting number of spaces in a given sentence with a function?"
 */

#include <iostream>

int spaces_in_sent(char *input);
int main(int argc, const char * argv[])
{
    char *s = "Planks can be manufactured in lengths to span the roof and are typically of powder coated aluminum hollow sections connected with hoses.";

    int i = 0;
  i =  spaces_in_sent(s);
    std::cout << "\n\n";
    std::cout << "There are " << i << " spaces in the above sentence. \n";
    
    return 0;
}


int spaces_in_sent(char *input){
    
    int i = 0;
    
    while (*input)
    {
        std::cout << *input;
        
        if (*input == ' ')
        {
            i++;
        }
        
        input++;
    }

    return i;
}
Last edited on
NOTE:
Your first "beginners" mistake is to mismatch your function name with its contents. CountChars() should not ask the user for the data, it should just count the chars. mixing functionality like this will lead you (and others reading your code) to confusion.

firstly you declare countChars differently to how you define it, these must be the same.
1
2
void countChars();
void countChars(int spaces, int totalchars, string input)


both are wrong if you want countSpaces() to return you something. try this instead:
void countChars(int& spaces, int& totalchars, string input);

now spaces and totalchars are out parameters so you can say
1
2
3
4
int numspaces,numchars;
countChars(numspaces,numchars,theString);

cout << "Found "<< numspaces << " in " << numchars, << " chars.";


then countChars() would need to look something like this... (note totalchars doesn't need to exist, main() could have called input.length() before or after the call to countChars )

1
2
3
4
5
6
7
8
void countChars(int& spaces, int& totalchars, string input)
{
    numspaces = 0;                               // start counting at zero
    totalchars = input.length();
    for (int i=0; i< totalchars; ++i)
        if ( isspace(input[i]) )
           ++numspaces;
}
Last edited on
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
#include <iostream>
#include <string>

int countSpaces(std::string&);

int main() {

    std::string str = "";

    std::cout << " Input a line please : ";
    std::getline(std::cin, str);

    std::cout << "There are " << countSpaces(str)
              << " spaces in this line" << std::endl;

    return 0;
}

int countSpaces(std::string& str) {

    int spaces = 0;

    for(auto &c : str) {
        
        if( c == ' ')  spaces++ ;
    }

    return spaces;
}
Last edited on
Thanks for the help, this is what I have now, but I'm still getting a few errors.

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
#include <iostream>
#include <string>

using namespace std;

void countChars();

int main()
{
	int numspaces;
	string userinput;
	
	cout << "Enter a sentence: " << endl;
	cin >> userinput;

	cout << "The number of spaces is " << countChars() << endl;

	return 0;
}

void countChars(int& spaces, int& totalchars, string input)
{

	spaces = 0;                     
	totalchars = input.length();
	for (int i = 0; i< totalchars; ++i)
		if (isspace(input[i]))
			++spaces;
	cout << spaces;
}


(note totalchars doesn't need to exist, main() could have called input.length() before or after the call to countChars )


You're probably right, but that's what I have to do for this assignment.
Ok, its looking better,

You still dont pass your data to countChars() though.

and it looks like you tried to blend more than one solution.

there are 2 ways to get results back from a call. it can return it, or it can pass it out in an out parameter.

my suggestion would be used like this
1
2
3
4
5
6
7
 int numspaces, totalchars;
string userinput;
cout << "Enter a sentence: " << endl;
cin >> userinput;
countChars(numspaces,totalchars,userinput);
cout << "The number of spaces is " << numspaces << endl;
return 0;


however, looking at your latest code, i would suggest the following...
you are using countChars() like a variable in main, so make it return a value
instead. also we can get rid of the parameters that you dont need in main.
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
int main()
{
	string userinput;
	
	cout << "Enter a sentence: " << endl;
	cin >> userinput;

        // countChars needs to know the string its counting
	cout << "The number of spaces is " << countChars(userinput) << endl;

	return 0;
}

// this returns just the number of chars now, which is an int. no out parameters
int countChars(string input)
{
       // these are local int's instead of parameters 
       // now because we dont need them anywhere else
	int spaces = 0;                     
	int totalchars = input.length();

	for (int i = 0; i< totalchars; ++i)
		if (isspace(input[i]))
			++spaces;
        return spaces; // dont cout it here, this function is called countchars not printnumchars()
}



I don't normally write code for newbies, but you've demonstrated self learning so hats off to you!
Well, I appreciate it. Thanks a bunch!
It compiles, but it always says the number of spaces is 0. It must be something in countChars I have to fix.
replace
cin >> userinput ;
with
getline(cin,userinput);
It works! Thank you all again!
Topic archived. No new replies allowed.