counting repeated words in input

Discussion in 'C++' started by arnuld, Aug 3, 2007.

  1. arnuld

    arnuld Guest

    /* C++ Primer 4/e

    * exercise 6.12, page 208
    * STATEMENT
    * write small programme to read a sequence of strings from
    standard input
    * looking for duplicated words. the programme should find places in
    the input where
    * one word is followed immediately by itself. keep track of the
    largest number of
    * times a single repetition occurs and which word is repeated . print
    the maximum
    * number of duplicates or else print the message that no word was
    repeated. e.g for
    * the input of:
    * how cow now now how cow cow now now now how how
    * the output should say: "now" occurs 3 times
    *
    */

    #include<iostream>
    #include<string>

    int main()
    {
    int current_cnt = 0;
    int final_cnt = 0;

    std::string current_string, prev_string, final_string;
    /* all strings are initialised to empty strings */

    while(std::cin >> current_string)
    {
    /* we can compare "current_string" to "final_string"
    because "final_string" was initialised by its default contructor
    to empty string */
    if(current_string == prev_string)
    {
    ++current_cnt;
    final_cnt = current_cnt;
    final_string = current_string;
    }
    else
    {
    current_cnt = 0;
    }
    }


    /* print the desired output */

    if(final_cnt == 0)
    {
    std::cout << "no word was repeated" << std::endl;
    }
    else
    {
    std::cout << "word "
    << final_string
    << " was repeated "
    << final_cnt
    << " times"
    << std::endl;
    }

    return 0;
    }


    this programme compiles and runs without any error but it has has a
    semantic-bug. it never counts the repeated words and no matter what
    you input it always provides one output: "no word was repeated".

    i am am unable to trace the reason of bug. any ideas ?
     
    arnuld, Aug 3, 2007
    #1
    1. Advertisements

  2. arnuld

    Colander Guest


    I think somewhere, somehow you should assign something to
    'prev_string'.
     
    Colander, Aug 3, 2007
    #2
    1. Advertisements

  3. arnuld

    Zeng Fucen Guest


    Yes ,I agree with Colander ,
    see 'prev_string' in the 'while' loop
     
    Zeng Fucen, Aug 3, 2007
    #3
  4. arnuld

    arnuld Guest

    Colander thanks. here is the corrected code, any advice on improvement ?

    /* C++ Primer 4/e

    * exercise 6.12, page 208
    * STATEMENT
    * write small programme to read a sequence of strings from
    standard input looking for duplicated words. the programme should find
    places in the input where one word is followed immedoately by itself.
    keep track of the largest number of times a single repitition occurs
    and which word is repeated . print the maximum number of duplicates or
    else prin tthe message that no word was repeated. e.g for the input of:
    how cow now now how cow cow now now now how how the output shpuld
    say: "now" occures 3 times
    */

    #include<iostream>
    #include<string>

    int main()
    {
    int current_cnt = 0;
    int final_cnt = 0;

    std::string current_string, prev_string, final_string;
    /* all strings are initialised to empty strings */

    while(std::cin >> current_string)
    {
    /* we can compare "current_string" to "final_string"
    because "final_string" was initialised by its default contructor to empty
    string */
    if(current_string == prev_string)
    {
    ++current_cnt;
    }
    else
    {
    current_cnt = 0;
    }

    if(current_cnt > final_cnt)
    {
    final_cnt = current_cnt;
    final_string = current_string;
    }

    prev_string = current_string;
    }


    ++final_cnt;
    /* because "final_cnt" will always start counting from 1 when a word
    has entered 2 times already. an ugly fix though :( */

    /* print the desired output */

    if(final_cnt == 0)
    {
    std::cout << "no word was repeated" << std::endl;
    }
    else
    {
    std::cout << "word '"
    << final_string
    << "' was repeated "
    << final_cnt
    << " times"
    << std::endl;
    }

    return 0;
    }

    ======= OUTPUT ===========
    ~/programming/cpp $ ./a.out
    cow
    cow
    noe
    now
    now
    now
    how
    how
    cow
    cow
    word 'now' was repeated 3 times
    ~/programming/cpp $
     
    arnuld, Aug 3, 2007
    #4
  5. In addition to what Colander and Zeng Fucen said you should probably
    also check if the current repetition is the longest one, in other words
    check if current_cnt is larger than final_cnt before assigning to it.
     
    =?ISO-8859-1?Q?Erik_Wikstr=F6m?=, Aug 3, 2007
    #5
  6. arnuld

    osmium Guest

    Does not have

    #include <map>

    Which is, IMO, the proper way to solve the problem described in the header.

    <snip>
     
    osmium, Aug 3, 2007
    #6
  7. The propriety of the way depends on the knowledge the book assumes
    accumulated by the time the exercise is presented. If 'std::map'
    has not been discussed before chapter 6, it cannot be used, AIUI.

    V
     
    Victor Bazarov, Aug 3, 2007
    #7
  8. arnuld

    osmium Guest

    That's a good point which I didn't consider. BTW, I don't even know what
    book is being discussed, I think there are at least two books with that
    title. But my general feeling is that you shouldn't give a problem that
    can't be solved "properly". I can see isolated instances where this rule is
    not followed, but I think such cases should be few and far between.
     
    osmium, Aug 3, 2007
    #8
  9. Both have the specific exercise on the specific page? Come on!
    I don't agree at all. According to you[r feeling], solving a problem
    numerically is "improper" if an analytical solution exists? Propriety
    depends on many things, and choosing a proper method should be even
    taught separately, and to achieve the ability to deduce propriety of
    some method, the method should actually be known/familiar. That's why
    without knowing all of them how do you learn what's "proper"?
    How "isolated" do you see them? Should bubble sort ever be taught?

    V
     
    Victor Bazarov, Aug 3, 2007
    #9
  10. arnuld

    Colander Guest

    Actualy I don't, it should work properly now! As for the 'ugly'
    correction you
    do, it isn't that ugly, it's well documented in your comment. That's a
    very
    clean way to solve 'strange' or 'ugly' pieces of code, compliments!

    Have fun coding,
    Colander
     
    Colander, Aug 3, 2007
    #10
  11. If the task was to get the total number of occurrences of a word, then a
    map would certainly be the best choice. But if asked for the number of
    repetitions then I don't think that a map is a good choice, not even if
    asked to count all repetitions. Maybe I missed something.
    The other book, I believe, is called C++ Primer Plus, not the best
    choice of name I agree.
     
    =?ISO-8859-1?Q?Erik_Wikstr=F6m?=, Aug 3, 2007
    #11
    1. Advertisements

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.