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 ....
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 ....
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.
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; }
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.....
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.
Thats why I am here ...I need idea what to do next ...
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.
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.
No reversing words not character .....
input: HELLO MY NAME
output: EMAN YM OLLEHThe output should be : NAME MY HELLO
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!!
I thought I will get ideas here ,well its unfortunate !!
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...
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.
Hope is the first step on the road to disappointment.