/*
" Counting number of spaces in a given sentence with a function?"
*/
#include <iostream>
int spaces_in_sent(char *input);
int main(int argc, constchar * 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;
}
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;
}
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.
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!