Problem with my reverse function and its output!

Discussion in 'Programming General' started by PoetGuy, Apr 29, 2008.

Problem with my reverse function and its output!
  1. Unread #1 - Apr 29, 2008 at 10:17 PM
  2. PoetGuy
    Referrals:
    0

    PoetGuy Guest

    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;  
     }  
    
    
    
     
  3. Unread #2 - Apr 30, 2008 at 6:30 PM
  4. tim
    Referrals:
    3

    tim Guest

    Problem with my reverse function and its output!

    I have had almost zero prior exposure to C but from what I can tell you're just dumping the entire files contents into one string, so when you're printing it out, it is displayed as such, i.e. on one line. An idea would be to make two seperate strings, and have each string contain one lines value(s).
     
  5. Unread #3 - May 1, 2008 at 4:57 AM
  6. Swan
    Joined:
    Jan 23, 2007
    Posts:
    4,957
    Referrals:
    0
    Sythe Gold:
    0
    Sythe's 10th Anniversary Member of the Month Winner

    Swan When They Cry...
    Retired Global Moderator

    Problem with my reverse function and its output!

    Wrong. Add the escape character '\n' every time you parse a new line. '\n' means Newline, just for your information. Just as '\r' means Return, '\a' means Alert (PC beep), and '\t' means tab space.

    Edit: Just so you know, tim, your method is extremely bad. Why would I want to create a new string for every single new line? Not very good practically, because then the user is limited to two lines.

    I haven't compiled this myself, but from what I can see, when the function is finished reversing the string, just add '\n' there. Recursion is an excellent way of doing this.
     
  7. Unread #4 - May 1, 2008 at 12:34 PM
  8. The Supreme Intelligence
    Joined:
    Apr 29, 2007
    Posts:
    738
    Referrals:
    0
    Sythe Gold:
    0

    The Supreme Intelligence Apprentice
    Banned

    Problem with my reverse function and its output!

    Exactly lmao. If you were able to write that program, you sure as hell should know the escape sequence for a new line.
     
  9. Unread #5 - May 2, 2008 at 2:43 AM
  10. Swan
    Joined:
    Jan 23, 2007
    Posts:
    4,957
    Referrals:
    0
    Sythe Gold:
    0
    Sythe's 10th Anniversary Member of the Month Winner

    Swan When They Cry...
    Retired Global Moderator

    Problem with my reverse function and its output!

    I have a tip for you.

    Use the various classes available with C++. They can help immensely. For example, the String and StringStream classes are quite the lifesaver in a situation like this.

    edit, feeling nice, have this:
    Code:
    #include <iostream>
    #include <fstream>
    #include <string>
    
    using namespace std;
    
    void ret_str(char* s)
    {
        if(*s != '\0')
             ret_str(s+1);
    
        cout<<*(s);
    }
    
    int main(int argc, char** argv) {
            string mystring;
            ifstream fstr("C:\\test.txt");
    
            char* s;
    
            while(!fstr.eof()) {
                    getline(fstr, mystring);
                    s = new char[mystring.length()];
                    strcpy(s, mystring.c_str());
                    ret_str(s);
                    cout << '\n';
            }
    
            fstr.close();
    
            return 0;
    }
    
    Can't remember who wrote the reverse function, it was just a snippet left on my computer. change "C:\\test.txt" to where ever your file is.
     
  11. Unread #6 - Jun 23, 2008 at 4:23 AM
  12. SidStudios
    Joined:
    Mar 14, 2007
    Posts:
    201
    Referrals:
    0
    Sythe Gold:
    0

    SidStudios Active Member

    Problem with my reverse function and its output!

< Rs programs | Basics of C++ >

Users viewing this thread
1 guest


 
 
Adblock breaks this site