Thread: Problem with my string reverse code

  1. #1
    Registered User
    Join Date
    Mar 2005
    Posts
    20

    Problem with my string reverse code

    Hi all, I was just playing around with strings, and I tried doing a string reverse using pointers, however, it did not turn out as I wanted. I tried using arrays and it works, but I cant do the same for pointers. As I'm still quite new to pointers, please guide me. Thank you. The code is as below:
    Code:
    #include <string.h>
    #include <stdio.h>
    #include <stdlib.h>
    
    char * reverseString(char *);
    
    int main()
    {
       char *input, *p;
       
       input = malloc(100);
       fgets(input, 100, stdin);
       if((p=strchr(input, '\n'))!=NULL)
          *p = '\0';
    	
       printf("%s\n", reverseString(input));
       
       return 0;	
    }
    
    char * reverseString(char *originalString)
    {
       char *temp;
       int length = 0;
       
       length = strlen(originalString);
       temp = malloc(length);
       
       temp = ???; /* I should assign temp to point somewhere? */
       
       /* assign pointer to point at end of string */
       originalString = &(originalString[length-1]);
       
       while(length)
       {
          *temp++ = *originalString--;
          length--;
       }
    
       return temp;
    }

  2. #2
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    Code:
    temp = malloc(length);
       
    temp = ???; /* I should assign temp to point somewhere? */
    No, after the malloc call, temp points to the memory that was allocated. If you repoint it somewhere else, you lose the address of the memory just allocated and end up causing a memory leak. I do however suggest you have another char pointer that you initialize to point to temp (after the malloc call) that you can use later within the loop to increment so you can safely return an unmodified temp pointer. You will also need to take into account the NULL terminating character which means you should have allocated an extra character, i.e. malloc(length+1).

    Code:
    while(length)
    {
        *temp++ = *originalString--;
        length--;
    }
    
    return temp;
    This is also problematic, you are modifying the value of the pointer you will eventually be returning after the loop (that's bad). In the above code, temp will eventually be pointing to the end of the string. Use the extra char pointer I mentioned above to increment your way through the characters. You are also not taking care of the null terminator I mentioned earlier.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  3. #3
    BellA_RoSa
    Join Date
    May 2005
    Posts
    8

    Lightbulb Hi

    it's been a while since I've reversed strings in the file the following code should work

    Code:
     
    // CH reads char from the file 
    // i is the offset of the file u r reading from ( end of the file) 
    // size is the offset i multiplied by -1 
    // *f2 is the file pointer we save the reversed string 
    // *f1 is the file pointer we read the string from 
    int Recursion( char CH , long i ,long SiZe, FILE *f2 , FILE *f1)
    {
      fputc(CH , f2); // Puts it in a new file
    // printf("%c" , CH);
      if ( i == SiZe   )
      return 0;
      
      i--;
      fseek(f1 , i , SEEK_END)  ;
      CH = fgetc(f1);
      printf("%c" , CH);
      Recursion( CH , i , SiZe ,f2 , f1);
    }
    // I'm not sure if the if statement is right c if it works it may print some spaces :P //

    Hope this makes things clearer for ya

  4. #4
    Registered User
    Join Date
    Mar 2005
    Posts
    20
    Thx hk_mp5kpdw! I tried assigning another char pointer to temp, and took into account of the NULL character, now it works perfectly. Thx again!

    Thx Bella_Rosa for the reply, will try your method too!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Compile Error that i dont understand
    By bobthebullet990 in forum C++ Programming
    Replies: 5
    Last Post: 05-05-2006, 09:19 AM
  2. Replies: 4
    Last Post: 03-03-2006, 02:11 AM
  3. Calculator + LinkedList
    By maro009 in forum C++ Programming
    Replies: 20
    Last Post: 05-17-2005, 12:56 PM
  4. Replies: 3
    Last Post: 03-27-2004, 12:15 PM
  5. creating class, and linking files
    By JCK in forum C++ Programming
    Replies: 12
    Last Post: 12-08-2002, 02:45 PM