Thread: reversing a string recursively?

  1. #1
    Registered User
    Join Date
    Jul 2009
    Location
    Croatia
    Posts
    272

    reversing a string recursively?

    Could anyone help me writing this function recursively?

    I know how to write it without recursion.

    Code:
    void Reverse2(char s[])
    {
         int c, i, j;
         
         for(i = 0, j = strlen(s)-1; i<j; i++, j--)
         {
             c = s[i];
             s[i] = s[j];
             s[j] = c;
         }
    }

  2. #2
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,059
    Search the cboard for plenty of similar posts.

  3. #3
    Registered User
    Join Date
    Jul 2009
    Location
    Croatia
    Posts
    272
    Code:
    void reverse(char *s, int len)
    {
    
         if(len <= 0)
            return;
         
         int temp = s[0];
         s[0] = s[len-1];
         s[len-1] = temp;
         
         reverse(s + 1, len - 2);
         
    }
    I think i did it, but it uses pointers! Is there a way to do this without pointers?
    Last edited by Tool; 12-27-2009 at 08:59 AM.

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Tool
    I think i did it, but it uses pointers! Is there a way to do this without pointers?
    Not really. The problem is that since in many contexts an array is converted to a pointer to its first element, completely avoiding pointers when working with arrays is practically impossible.

    Of course, you can try to avoid pointer notation further by passing an index along with a pointer to the first character of the array, but that is not so elegant.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User
    Join Date
    Jul 2009
    Location
    Croatia
    Posts
    272
    What about this:

    Code:
    void reverse2(char s[])
    {
         static int i = 0;
         static int j = strlen(s);
         
         if(j <= strlen(s) / 2)
            return;
         
         char temp = s[i];
         s[i] = s[j-1];
         s[j-1] = temp;
         
         i++, j--;
         reverse2(s);
         
    }
    Uses static int instead of pointers.

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    That is just horrible. Does it even compile? If it does, how are you going to reuse the function?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #7
    Registered User
    Join Date
    Jul 2009
    Location
    Croatia
    Posts
    272
    Oh, your right. I forgot static works only for the first call.

    Ill just go with the pointer version then .

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. char Handling, probably typical newbie stuff
    By Neolyth in forum C Programming
    Replies: 16
    Last Post: 06-21-2009, 04:05 AM
  2. String issues
    By The_professor in forum C++ Programming
    Replies: 7
    Last Post: 06-12-2007, 09:11 AM
  3. Program using classes - keeps crashing
    By webren in forum C++ Programming
    Replies: 4
    Last Post: 09-16-2005, 03:58 PM
  4. Calculator + LinkedList
    By maro009 in forum C++ Programming
    Replies: 20
    Last Post: 05-17-2005, 12:56 PM
  5. Reversing a String
    By ToasterPas in forum C++ Programming
    Replies: 10
    Last Post: 08-14-2001, 12:20 PM