Counting words occurences

This is a discussion on Counting words occurences within the C++ Programming forums, part of the General Programming Boards category; Hi, I've just done an exercise from a book that counts the number of occurences of a word in an ...

  1. #1
    Registered User
    Join Date
    Feb 2009
    Posts
    329

    Counting words occurences

    Hi,

    I've just done an exercise from a book that counts the number of occurences of a word in an input. I have done this, and it works. However, i'm not sure if this is a good way to do it? I was a little confused at the start regarding the loop/vector off by one idea and tried to work around that. I am aware of strcmp, however I have not reached that yet in the book and as such, tried to do this without using that function. Any comments would be welcome.

    Code:
    void CheckWords(const Vector<string>& wds)
    {
    	int counter = 1;
    
    	for(int i = 0; i<wds.size()-1; ++i){
    		//check if word equal to next word, if so increment counter
    		if(wds[i] == wds[i+1])
    			++counter;
    		else{//if not equal, print the word and the number of occurences
    			cout << wds[i] << " Counter: " << counter << endl;
    			counter = 1;
    		}
    		//check if word is penultimate and if matches last word or not.
    		if(i == wds.size()-2){
    			if(wds[i] == wds[i+1]){
    				cout << wds[i] << "counter: " << counter << endl;
    			counter = 1;
    			}
    			if(wds[i] != wds[i+1]){
    				cout << wds[i+1] << "counter 1" << endl;
    				counter = 1;
    			}
    		}
    	}
    }

  2. #2
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by darren78 View Post
    I am aware of strcmp
    Strcmp is for C style char strings. With C++ you can just use the <, >, and == operators, or .compare().

    A logical problems with this:
    Code:
    void CheckWords(const Vector<string>& wds)
    {
    	int counter = 1;
    
    	for(int i = 0; i<wds.size()-1; ++i){
    		//check if word equal to next word, if so increment counter
    		if(wds[i] == wds[i+1])
    			++counter;
    You are detecting repeated words, not duplicated words. Eg, if the vector were "one", "two", "three", "two" you would not detect the two twos.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  3. #3
    Registered User
    Join Date
    Feb 2009
    Posts
    329
    Quote Originally Posted by MK27 View Post
    Strcmp is for C style char strings. With C++ you can just use the <, >, and == operators, or .compare().

    A logical problems with this:
    Code:
    void CheckWords(const Vector<string>& wds)
    {
    	int counter = 1;
    
    	for(int i = 0; i<wds.size()-1; ++i){
    		//check if word equal to next word, if so increment counter
    		if(wds[i] == wds[i+1])
    			++counter;
    You are detecting repeated words, not duplicated words. Eg, if the vector were "one", "two", "three", "two" you would not detect the two twos.
    Thanks for the response. I have previously sorted the vector, so that all identical words are next to each other.

    Apart from that, is the method correct , bearing in mind the contsraints I have put my self under to write the code?

    Thanks again.

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    23,614
    Just a note that you may find useful in the future: this particular problem can be solved by using a std::map to map words to their counts.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 11
    Last Post: 12-31-2007, 01:32 PM
  2. Counting Number of Words in a Text File Using C
    By wvu2005 in forum C Programming
    Replies: 16
    Last Post: 09-27-2005, 11:45 AM
  3. New Theme
    By XSquared in forum A Brief History of Cprogramming.com
    Replies: 160
    Last Post: 04-01-2004, 07:00 PM
  4. counting words in string
    By Unregistered in forum C Programming
    Replies: 9
    Last Post: 05-30-2002, 04:10 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21