Like Tree1Likes
  • 1 Post By quzah

Reversing word in a file

This is a discussion on Reversing word in a file within the C Programming forums, part of the General Programming Boards category; Hi all, I am really stuck with this problem, I need help ,ideas......I am reading data from a file and ...

  1. #1
    Registered User
    Join Date
    Aug 2011
    Posts
    59

    Reversing word in a file

    Hi all,

    I am really stuck with this problem, I need help ,ideas......I am reading data from a file and outputing it to another file in a reverse mode. my problem is how to reverse each word ....

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    So what have you tried? I'm pretty sure someone here has told you to think things through with actual words before, and to break it down into steps. If not, I'm telling you now.


    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    Aug 2011
    Posts
    59
    sorry ....I forgot to post what i have done so far !!!

    Code:
    
    #include <ctype.h>
    #include <stdbool.h>
    #include <stdio.h>
    #include <stdlib.h>
    
    int main(int argc , char **argv[ ] ) {
        char ch;
        FILE *input_file = fopen(argv[1], "r");
        if (!input_file) {
            printf("Please create a file named \"input.txt\""
                   " in the working directory.\n");
            return EXIT_FAILURE;
        }
        FILE *output_file = fopen ("output.txt", "w");
        
        fseek ( input_file , 0L , SEEK_END );
        long pos = ftell ( input_file );
    
        pos = pos - 1;
        while ( pos >= 0L ) {
            fseek ( input_file , pos , SEEK_SET );
            ch = fgetc( input_file );
            if ( ch==' ' ){
                fputc('\n', output_file);
            }
            else
                fputc ( ch, output_file );
            
            pos-- ;
    
        }
        fclose(input_file);
        fclose(output_file);
        
        return EXIT_SUCCESS;
    }

  4. #4
    Registered User
    Join Date
    Aug 2011
    Posts
    59
    This is reading the data from input and outputing it to output but my problem here is my code is reversing every characters instead of word.....

  5. #5
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    You need to be able to explain in words how to do the problem before you can program it.


    Quzah.
    Hope is the first step on the road to disappointment.

  6. #6
    Registered User
    Join Date
    Aug 2011
    Posts
    59
    Thats why I am here ...I need idea what to do next ...

  7. #7
    Registered User
    Join Date
    Sep 2011
    Posts
    111
    What do you mean by reversing every character?

    Is your program supposed to do something like

    input: HELLO MY NAME
    output: EMAN YM OLLEH

    ???
    Last edited by Strahd; 09-23-2011 at 09:57 PM.

  8. #8
    Registered User
    Join Date
    May 2009
    Posts
    2,507
    Edit: I was assuming the same problem as "Strahd"; the questions below still need answered; but other are needed. That are not obvious to me.

    Questions to ask yourself:

    What is a word?
    Does a non-word exist? (something that is not part of a word)
    If yes, what is a non-word?

    Edit:
    What happens to multi-line files?
    What happens to multi-sentence files?

    Did the class cover state machines? If yes, then you might think of the problem in terms of state machines.

    Tim S.
    Last edited by stahta01; 09-23-2011 at 10:08 PM.

  9. #9
    Registered User
    Join Date
    Aug 2011
    Posts
    59
    No reversing words not character .....

    input: HELLO MY NAME
    output: EMAN YM OLLEH
    The output should be : NAME MY HELLO

  10. #10
    Registered User
    Join Date
    Aug 2011
    Posts
    59
    I understand I have to stop reading a word after i see '\n' but I am confused going from the beginning of the word to the end and copying the word!!

  11. #11
    Registered User
    Join Date
    Aug 2011
    Posts
    59
    I thought I will get ideas here ,well its unfortunate !!

  12. #12
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Ok... to give you some starting direction...

    Think about loading the entire file into memory then scanning it in memory, in reverse, counting the lenth of each word (a group of characters separated by whitespace) and then writing the file out in chunks of one word...

    Shouldn't be all that hard to figure out from there...

  13. #13
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    There's a reason we all wanted you to stop and think in steps about how to solve the problem. If I wrote a sentence on a piece of paper and asked you to copy it to another with the words in reverse, how would you do it? If you can't answer that, you can't solve this problem.


    Quzah.
    CommonTater likes this.
    Hope is the first step on the road to disappointment.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. reading text-and-numbers file word by word
    By bored_guy in forum C Programming
    Replies: 22
    Last Post: 10-26-2009, 10:59 PM
  2. reading file word by word
    By 98holb in forum C Programming
    Replies: 2
    Last Post: 01-25-2006, 04:49 PM
  3. Reading in a file word by word
    By Bumblebee11 in forum C Programming
    Replies: 4
    Last Post: 06-10-2003, 09:39 PM
  4. open file, search of word, replace word with another
    By Unregistered in forum C++ Programming
    Replies: 0
    Last Post: 06-05-2002, 01:16 PM
  5. Help reading text file word by word
    By Unregistered in forum C++ Programming
    Replies: 6
    Last Post: 05-25-2002, 05:13 PM

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