Thread: Help with reverse string in recursion

  1. #1
    Registered User
    Join Date
    Feb 2016
    Posts
    11

    Help with reverse string in recursion

    having trouble with reversing a string using recursion, i want my function to take 1 parameter which is a pointer to the string and reverse it recursively,if possible.
    here is what i have now:
    Code:
    void rev(char *str){
    char t;
    char *s=str;//point to the first character
    char *e=str+(strlen(str)-1);//point to the last character
    if(s>e)
    {
    return;
    }
    rev(str++);/*<---here is my problem,now the last character will be the same one after the first call and i dont want that i want it to be the character before the last and so on,any ideas?*/
    t=*s;
    *s=*e;
    *e=t;
    }
    Last edited by Stark88; 02-28-2016 at 05:09 AM.

  2. #2
    Registered User
    Join Date
    Feb 2016
    Posts
    11
    i have solved it like this by adding a static variable but i don't like it,if you have any ideas i would be grateful.
    Code:
    void rev(char *str){
    	static int i = 0;
    	char t;
    	char *s = str+i;
    	char *e = str+(strlen(str) -i-1);
    	if (s>e)
    	{
    		return;
    	}
    	i++;
    	rev(str);
    	t = *s;
    	*s = *e;
    	*e = t;
    }

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Stark88
    i have solved it like this by adding a static variable but i don't like it,if you have any ideas i would be grateful.
    You are right to dislike the use of a static variable. A better idea is to make use of a helper function:
    Code:
    static void rev_helper(char *str, size_t len) {
        if (len > 1) {
            /* swap and do the recursive call
               ...
             */
        }
    }
    
    void rev(char *str) {
        rev_helper(str, strlen(str));
    }
    Note that you do not need to call strlen in rev_helper because the current string length is already known via the parameter. Additionally, I declared the helper function static since it is implementation detail purely to assist the implementation of rev: you certainly would not declare it in a header file.
    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

  4. #4
    Registered User
    Join Date
    Feb 2016
    Posts
    11
    Awesome thanks for the help much appreciated.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Reverse string using recursion
    By Satya in forum C Programming
    Replies: 1
    Last Post: 06-24-2015, 05:08 AM
  2. Reversing LinkList In Reverse Order Using Recursion
    By nickman in forum C Programming
    Replies: 6
    Last Post: 05-06-2014, 11:57 PM
  3. reverse a string without string.h library
    By antros48 in forum C Programming
    Replies: 6
    Last Post: 09-10-2011, 06:01 PM
  4. Reverse a string (without using any string functions?)
    By geetard in forum C Programming
    Replies: 2
    Last Post: 11-15-2006, 07:42 PM
  5. string in reverse
    By Teele777 in forum C Programming
    Replies: 2
    Last Post: 04-18-2002, 07:27 PM

Tags for this Thread