problem in counting total number of words in sentence

hi

i am trying to find out total number of words in a sentence but i am facing a problem. when i run the following code and enter the string in for example
hi how are you. i am fine. whats up#
then it displays the number of words fine
Total: 9
when enter this input
hi how are you. i am fine. whats up #
notice there is space between "up" and "#"
then its output is
Total: 10
which is wrong
and when i enter this
1
2
3
hi how are you
i am fine
whats up# 

output is
Total: 7
this is also wrong

here is my code
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <iostream>
#include <string>

string inputString = "";
int count = 1;

int main()
{
	cout << "Enter String: ";
	getline(cin,inputString,'#');
	//cout << inputString.length() << endl;
	for( int i = 0; i < inputString.length(); i++) {
		if(inputString[i] == ' ') {
			count = count + 1;
		}
	}
	cout << "Total: " << count;
	return 0;
}

please help
Last edited on
Well one problem is you are only accounting for ' ' spaces, so when you push return (enter) you don't account for the fact that it will not be a space but a '\r' or '\n' (can't remember which)

Another problem is you are saying anything counts as a word as long as it's not a ' ' so when you have "whats up #" you do in fact have three words, because "#" is a word by your definition.

You may want to change your cin functionality- here's a useful function :

1
2
3
4
5
6
7
8
9
10
11
12
string cin_all() { // get's input until a return is pressed
  string endstr = "";
  string tempstr = "";
  do {
    if(cin.peek() == ' ')
      endstr += " ";
    cin >> tempstr;
    endstr += tempstr;
  }
  while(cin.peek() == ' ');
  return endstr;
}


and your counting you may want to change in this way:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
int word_count(string testrt){
    int charnum = 0;
    int wordnum = 0;
  for (int temp = 0; temp < testrt.length(); temp++){
      if (isalpha(testrt[temp])){
       charnum++;
      }
      else{
       if (charnum > 0){
        wordnum++;
        charnum = 0;
       }
      }
  }
  return ++wordnum;
}
@ultifinitus: '\n' is the new line character not '\r'.
#include <cctype> and use the isspace() function to determine if a character is whitespace.

When counting words, you need to consider the possibility that there is leading and trailing whitespace in your string.

@ultifinitus
The getline() function does not save the '#' in the result string -- it is read and discarded. Hence, it is not being counted as a word.


I personally prefer to just ask a person to press ENTER twice to finish.

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

typedef std::string paragraph;

std::istream& operator >> ( std::istream& ins, paragraph& p )
  {
  std::string s;

  p.clear();
  while (getline( ins, s ) && !s.empty())
    {
    if (!p.empty()) p += '\n';
    p += s;
    }

  return ins;
  }

int main()
  {
  using namespace std;

  paragraph pp;

  cout << "Please enter a paragraph. Press ENTER twice to finish:\n";
  cin >> pp;

  cout << "Good job. Your paragraph is:\n\"" << pp << "\"\n";

  return 0;
  }

Hope this helps.
Duoas said:
The getline() function does not save the '#' in the result string -- it is read and discarded. Hence, it is not being counted as a word.


Ha, my bad- thanks for the info.
the problem is i have to use # to stop getting input from user and it must get input in multiple line.
Last edited on
Yes, the way you are doing it is fine: getline(cin, inputString, '#');
Thank you, Duoas. It's nice to see an example of how an advanced function like typedef helps to make the code more readable without having to use extra comments to explain what's going on.

Once you overload the operator '>>' , how do you go back to it's standard implementation?
It still works.

1
2
3
4
cin >> anInt;
cin >> myParagraph;
cin >> aString;
...
Topic archived. No new replies allowed.