Problem with my reverse function and its output!

This is a discussion on Problem with my reverse function and its output! within the C Programming forums, part of the General Programming Boards category; Hey as some of you might have read on my previous thread, i was working on a little program. I ...

  1. #1
    POeT GuY Matus's Avatar
    Join Date
    Feb 2008
    Location
    Bz
    Posts
    235

    Problem with my reverse function and its output!

    Hey as some of you might have read on my previous thread, i was working on a little program. I got it working now, however the way its throwing out its output is not the way its supposed to be.

    This file will be essentially the same as the original file, but will have all characters on all lines the reverse of the original. For example:
    The original file has:

    Jack Spratt
    could eat no fat.


    The output file will look like:

    ttartpS kcaJ
    .taf on tae dluoc


    But my program is throwing its output like this in one line:
    ttartpS kcaJ.taf on tae dluoc

    Can anyone assist me in fixing this, so it prints it in the correct format: Here are the codes:

    Code:
    #include <stdio.h> 
    #include <string.h> 
    #include <ctype.h>  
          
    // Our function declaration saying it takes one string pointer and  returns one string pointer.  
    char * flipstring(char *stringtoflip);  
       
    int main() {  
    //  Strings to hold our lines (forward and backward)  
        char  string[256];  
        char flipped[256];  
       
    //  Our file pointers (one in and one out)  
        FILE * infile;  
        FILE * outfile;  
       
      // Open files to valid  txt files  
        infile = fopen("input.txt","r");  
       
        outfile = fopen("output.txt","w");  
       
      // Loop  through the input file reading each line up to 256 chars  
       
        while (fgets(string,256,infile) != NULL) {  
        printf("The  value read in is: %s\n", string);  
       
      // Flip the  string and copy it to our "flipped" string  
       
        strcpy(flipped,flipstring(string));  
       
      // Write  "flipped" to output  
      printf("The value written is:  %s\n",flipped);  
      fputs(flipped,outfile);}  
       
      // Close files  
      fclose(infile);  
      fclose(outfile);  
       
      return 0;  
     }  
        
     // Flips strings by taking incoming   string, loop in reverse  
      // and write each char to reverse  string. Return reverse string.  
    char * flipstring(char *stringtoflip) {  
      
      static char reverse[256];  
        
        
      int j = 0;  
      int i= strlen(stringtoflip)-1;
        
       // Loop through each char of our  string in reverse  
       for( i ;i>=0; i--)  
       {  
       // If it is a newline, just  ignore for now.  
       if (stringtoflip[i] != '\n') {  
        
      reverse[j]=stringtoflip[i];  
       j++;  
       }  
        
      }  
       
       // Terminate the new reverse string.  
       reverse[j] = '\0';  
        
       // Return reverse  string back to main()  
        return reverse;  
     }

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Write a newline character to the file every time you write a line.

  3. #3
    POeT GuY Matus's Avatar
    Join Date
    Feb 2008
    Location
    Bz
    Posts
    235
    Quote Originally Posted by tabstop View Post
    Write a newline character to the file every time you write a line.
    One small question, i know i should do that, but how or where should i write it??

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    You should just write it with fputs like the other string. Write out the string with fputs, and then write out "\n" with fputs.

  5. #5
    POeT GuY Matus's Avatar
    Join Date
    Feb 2008
    Location
    Bz
    Posts
    235
    Quote Originally Posted by tabstop View Post
    You should just write it with fputs like the other string. Write out the string with fputs, and then write out "\n" with fputs.

    Thanks a lot lot lot lot lot, really appreciate the help i got from you today. If this place would be like yahoo answers id give u the ten points , lol. Well thanks a lot, if u ever need help in some other stuff, id be glad to help, i may not be the expert as yet in programming, but im heading there little by little, however im good with english so let me know if anything.

    Cheers! Work all done!

Popular pages Recent additions subscribe to a feed

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