0

This is what I am trying to do, this is not nearly the entire program I am making just a small portion. This part is counting the # of words of what I enter.

string s;
getline(cin, s);

for (unsigned int i = 0; i < s.length(); i++)
{
    if (s[i] == ' ' || s[i] == '.')
    {
        numWords++;
    }
}

This obviously works if someone enters a correct line with a period at the end. I can't think of how to get it to recognize the end of line character though, incase they don't put a period.

  • how about newline -- \n character ? Also, I'm pretty sure there are standard input processing functions (its been a while for me with C++ or C# etc) for handling newlines. And lastly - a period is a terrible signifier for the end of a line, for example what if the input contained two sentences? – Ross Sep 16 '13 at 23:14
  • that doesn't count the last word then though. if i input "hello" it will tell me 0 words – diggindog Sep 16 '13 at 23:17
  • Nah I want to know the total amount of words in the input. – diggindog Sep 16 '13 at 23:25
  • What is wrong with the code you have? It seems that you are able to count number of words just by looking at number of spaces entered. Then at the end just add 1 to the total. Ex: `What is wrong with the code you have?` using your code, I get 7 + 1 = 8 – smac89 Sep 16 '13 at 23:30
  • "Hello Dr. Smith. It's nice to meet you." 8 words, or 11? – Tim Sep 16 '13 at 23:34
  • ah thanks @Smac89. I didn't think of that because it wouldn't work if there was no words entered. But by checking if !s.empty() I can only add 1 if there is something in the string. – diggindog Sep 16 '13 at 23:36
  • This way it will only works if words are seperated by **exactly** 1 space or dot. So you cannot count words like this. Better jumps over continuous spaces – phuclv Sep 17 '13 at 01:14

4 Answers4

0

Why not just start the numWords counter on 1 and only count the spaces?

Gyran
  • 115
  • 1
  • 6
  • Cuz if I don't enter anything, it will say there is 1 word. – diggindog Sep 16 '13 at 23:14
  • See this on string explosion: http://stackoverflow.com/questions/8448176/split-a-string-into-an-array-in-c you could use this to explode the words and then check the sizeof the array. – Ross Sep 16 '13 at 23:16
0

You can simply count the number of " " in your line and add that to 1. That's all.

Chaos
  • 374
  • 2
  • 11
0
  bool previousSpace = false;
  for (unsigned int i = 0; i < s.length(); i++)
  {
    if (isspace(s[i]) || s[i] == '.')
    {
        if (previousSpace == false)
        {
          numWords++;
          previousSpace = true;
        }
    }
    else
    {
      previousSpace = false;
    }
  }
  cout << numWords + (previousSpace ? 0 : 1) << endl;

This considers more than one consecutive spaces. The key here is to use isspace function.

thefourtheye
  • 233,700
  • 52
  • 457
  • 497
-1

Could this be the same that you are after? Count the number of times each word occurs in a file or this already duplicate one Word count program in C++ duplicate?

Community
  • 1
  • 1
Magnus
  • 379
  • 1
  • 15