Thread: Program to count each distinct word input

  1. #1
    Registered User
    Join Date
    Jun 2014
    Posts
    11

    Program to count each distinct word input

    I am trying to solve this problem:
    "Write a program to count how many times each distinct word appears in it's input" Accelerated C++ p49, exercise 3-3. So far, I have placed all the words input into a vector:

    Code:
    #include <iostream>
    #include <string>
    #include <vector>
    
    
    using std::cin;
    using std::cout;
    using std::endl;
    using std::string;
    using std::vector;
    
    
    int main()
    {
      // program to count number of times each distinct word appears in input
    
    
      // first - get all input from user
      cout << "Please enter some words: (Ctl-D to end input) " << endl;
      vector<string> words;
      string word;
      while(cin >> word)
        {
          words.push_back(word);
        }
    
    
      // test that some words have been entered
      typedef vector<string>::size_type vec_sz;
      vec_sz size = words.size();
      if (size == 0)
        {
          cout << endl << "You must enter 1 word or more. Please try again. " << endl;
          return 1;
        }
    
    
      // Now all words have been entered, the data processing can be done
      
    }
    What should I do next? I am thinking I should start with first element of vector and compare it to all other elements. If the string is repeated then add 1 to count for each repeated word. The problem is that my solution is not clear. I don't know if I need a separate vector to hold the count for each word or if count can just be an int? I just cant get anywhere with this and am worried as it is supposed to be a simple exercise that i should be able to do without help?

  2. #2
    Informer -Adrian's Avatar
    Join Date
    Jan 2013
    Posts
    830
    The difficulty in giving you advice here is that I don't know which features of C++ you have covered already. Sometimes books want you to come up with a nifty little algorithm using the features you have learned so far. In that cause more "elegant" solutions using C++ standard library features might be missing the mark. For instance, if you're allowed to use sorting, then you could sort your word vector, thereby packing all identical words together. Then it would just be a matter of printing the first word, advancing through the vector (counting up) while the words remain the same and printing that number as soon as a non-identical word comes up; lather, rinse, repeat.

    I like that you're already using size_type. If you want you can look up the auto keyword which can deduce the type for you (via the assignment).
    Code:
    auto size = words.size(); // gee, I must be a vector<string>::size_type!
    In your case you could also do:
    Code:
    if (words.size() == 0)
    { ... }
    
    // or better yet
    if (words.empty())
    { ... }
    Happy coding!
    Last edited by -Adrian; 06-30-2015 at 09:54 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help keeping a count of distinct values
    By Kevin Nguyen in forum C Programming
    Replies: 9
    Last Post: 09-13-2013, 12:31 AM
  2. Word count program
    By Jpeg6 in forum C Programming
    Replies: 1
    Last Post: 10-18-2010, 10:34 PM
  3. Replies: 2
    Last Post: 07-28-2010, 02:07 PM
  4. Count distinct array values
    By rkooij in forum C Programming
    Replies: 4
    Last Post: 10-03-2006, 03:03 AM
  5. word count program need a bit of help!
    By Unregistered in forum C Programming
    Replies: 7
    Last Post: 04-19-2002, 08:15 PM