Thread: strtok help on a word frequency program

  1. #1
    Registered User
    Join Date
    Jul 2010
    Posts
    1

    strtok help on a word frequency program

    Hello, I'm new to C++ and I am stuck on a problem I was assigned. Below is the problem statement and I will post my code as well.

    Write a C++ program that reads one line of text from the user via the keyboard, then reports the frequency of the word lengths found in that line of text. Here is a sample run of the program. The program first asks the user to enter a line of text:

    Enter a line of text:

    Then, the user types in a line such as this one (the user can type in any text of up to 80 characters):

    The cat ran down the road. Meanwhile, a storm was gathering.


    Requirements

    1. Put your solution in a file name "WordFreq.cpp". You can have other files if you wish (the main function should be in WordFreq.cpp).
    2. The user can type up to an 80 character line. Therefore, the longest possible word can be 80 characters.
    3. Your "Word Frequency Report" should report up to and including the longest word found, and not beyond that length. For example, if the longest word in the user's input is three characters, then the report should go from 1 to 3. If the longest word in the user input was 21 characters, then the report should go from 1 to 21. In the sample run of the program, above, the longest words were of length ten, so the report went from 1 to 10. The longest possible word is 80 characters. In the second sample run of the program, below, the longest word was five characters so only word lenghths of 1 to 5 were reported. Your program should automatically adjust the length of the report.

    Hints

    1. You can use the cin.getline function to read a line of text from the user. See the textbook for details on getline.
    2. You can use function strtok to break a line into its separate words. I posted a video on how to use strtok that you might want to look at (strtok is also covered in the textbook).
    3. Get an early start on the program. If you get stuck you can send me your code via email.


    Here is my code.
    Code:
    #include "stdafx.h"
    #include <iostream>
    #include <cstring>
    #include <iomanip>
    using namespace std;
    
    
    
    int main()
    {
    	int wordCount = 0;
    	int letterCount = 0;
    	
    	cout << "Enter a line of text" << endl;
    	char text[80];
    
        cin.getline(text,80);
        char *p = strtok(text, " ");
    
    	while (p != NULL)
    	{
    	        
    	  p = strtok(NULL, " ");
    	  letterCount = 0;
    	  for ( int i =0; text[i] != '\0'; i++)  
    	
    	  {
    		  letterCount ++;	  
    		  
    	  }
           
           cout << letterCount << endl;
    	   
    	}
     	
    	return 0;
    }
    Like I said I'm new to programming all together so this seems very difficult me. My output for that just counts the first token and also the amount of words in the input. So if someone was to input...."I hate this program" the output would read....
    1
    1
    1
    1 1 being the amount in the first token and repeated 4 times for the amount of words in the input. I basically need to figure out how to get past the first token, and also how to implement the array and make a count on said array.

    Sorry if this is long.

  2. #2
    Kiss the monkey. CodeMonkey's Avatar
    Join Date
    Sep 2001
    Posts
    937
    "You can use function strtok"

    Does that mean that you also can use other methods? Isn't this a c++ class? Your header includes are c++-style. I'm probably not helping but what about this?
    Code:
    #include <map>
    #include <string>
    #include <sstream>
    #include <iostream>
    
    int main()
    {
       std::string sentence;
       std::getline( std::cin, sentence);
       
       std::istringstream iss(sentence);
       std::map< unsigned int, unsigned int > length_freqs;
    
       std::string word;
       while(iss >> word)
       {
          Remove_Punctuation(word);
          ++length_freqs[ word.size() ];
       }
    
       for( std::map<unsigned int, unsigned int>::iterator i = length_freqs.begin();
              i != length_freqs.end();
              ++i )
       {
          std::cout << '(' << i->first << ',' << it->second << ")\n";
       }
    
       return 0;
    }
    "If you tell the truth, you don't have to remember anything"
    -Mark Twain

  3. #3
    Registered User
    Join Date
    Nov 2008
    Posts
    30
    You can use strlen to count the string length. So, you can get rid of the for loop.
    Also, when you call strtok in the while loop, you are overwriting the first substring pointer gotten using the call to strtok before the while loop. So, move the second call to strtok at the end of while loop like this:
    Code:
        char *p = strtok(text, " ");
    
    	while (p != NULL)
    	{
    	  letterCount = strlen(p);
          cout << letterCount << endl;
    	  p = strtok(NULL, " ");
    	}

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. word count program not working
    By vsovereign in forum C Programming
    Replies: 5
    Last Post: 06-04-2010, 02:11 PM
  2. A C++ program problem.
    By hector_ronado in forum C++ Programming
    Replies: 1
    Last Post: 11-28-2009, 09:26 AM
  3. Phone number to word conversion program
    By lostmyshadow in forum C Programming
    Replies: 12
    Last Post: 04-21-2009, 02:31 PM
  4. brace-enclosed error
    By jdc18 in forum C++ Programming
    Replies: 53
    Last Post: 05-03-2007, 05:49 PM
  5. Wrong Output
    By egomaster69 in forum C Programming
    Replies: 7
    Last Post: 01-28-2005, 06:44 PM