sort and reading a textfile

Discussion in 'C++' started by BjoernJackschina, May 7, 2004.

  1. Hello,
    I just look for a capability to sort several words in view of
    alphabet.
    An example:
    stop is 'opst'
    reach is 'aechr'
    This should read in a new file so that I can look for same letter
    orders. When I find the same orders it is an anagram.
    My problem is to sort and to select the textfile. I need dataset for
    dataset. What is the best way to realize that.

    Many thanks for answer
     
    BjoernJackschina, May 7, 2004
    #1
    1. Advertising

  2. BjoernJackschina wrote:
    >
    > Hello,
    > I just look for a capability to sort several words in view of
    > alphabet.
    > An example:
    > stop is 'opst'
    > reach is 'aechr'
    > This should read in a new file so that I can look for same letter
    > orders. When I find the same orders it is an anagram.
    > My problem is to sort


    take the word and split it into a vector of characters.
    sort that vector.
    create a new string from the sorted vector

    > and to select the textfile.


    not sure what you mean by that.

    > I need dataset for
    > dataset. What is the best way to realize that.


    What do you have already?
    It's much simpler to tailor an answer to somebody if we
    know what level of knowledege we can expect from you and
    if we know what parts you can solve on your own.

    Usually newbies make the same mistake over and over: They
    write to much in one big rush. Divide your problem into
    subproblems and solve them. Be prepared to throw away
    code.

    eg.

    you could start with:

    enter a word from the user
    sort the letters
    output the letters

    Test what you have so far

    move the sorting functionality into a function of its own.

    Test what you have so far

    open a specific file
    read a word
    output the word for checking
    sort the word (using the previously written function)
    output the sorted word
    close file

    Test what you have so far

    modify the inner workings of your program to not read
    one word but to read all words in a loop

    Test what you have so far

    replace the fixed filename with some logic to ask the
    user for the filename

    *** Whole assignment done ****

    The important part is: Do small modifications to your program
    and test them!

    Be prepared: the above smells like homework. So you need
    to show code snippets or your whole program and ask specific
    questions about that.

    --
    Karl Heinz Buchegger
     
    Karl Heinz Buchegger, May 7, 2004
    #2
    1. Advertising

  3. (BjoernJackschina) wrote in message news:<>...
    > Hello,
    > I just look for a capability to sort several words in view of
    > alphabet.
    > An example:
    > stop is 'opst'
    > reach is 'aechr'
    > This should read in a new file so that I can look for same letter
    > orders. When I find the same orders it is an anagram.
    > My problem is to sort and to select the textfile. I need dataset for
    > dataset. What is the best way to realize that.
    >
    > Many thanks for answer


    Lucky for you, C++ has a number of types and algorithms ready for you.
    For instance, the string type and the sort algorithm will give you
    the "stop"->"opst" transformation, with one line of code:
    // std::string word = "stop"
    std::sort( word.begin(), word.end() );

    Now, what do you do with the textfile? Each word goes into a
    std::string, and each sorted word into another. You want to keep
    these together, so you can use a std::pair<std::string,std::string>
    for that. std::multimap<std::string,std::string> keeps a sorted
    collection of such pairs, so you can quickly lookup "opst".
    You'll then find the pair "opst","post".

    Regards,
    Michiel Salters
     
    Michiel Salters, May 7, 2004
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Digital Puer
    Replies:
    5
    Views:
    49,551
    Digital Puer
    Feb 23, 2006
  2. Markus Hofmann
    Replies:
    3
    Views:
    742
    Jerry Coffin
    Aug 12, 2003
  3. BjoernJackschina

    reading a textfile

    BjoernJackschina, May 9, 2004, in forum: C++
    Replies:
    2
    Views:
    458
    David Harmon
    May 10, 2004
  4. Marco Herrn
    Replies:
    3
    Views:
    502
    Marco Herrn
    May 17, 2006
  5. Navin
    Replies:
    1
    Views:
    761
    Ken Schaefer
    Sep 9, 2003
Loading...

Share This Page