Thread: Recursive string reversal

  1. #1
    Registered User
    Join Date
    Oct 2005
    Posts
    19

    Recursive string reversal

    I am trying to reverse a string using head() (first letter of a string) and tail() (all letters after the head) functions from a class I wrote. Everything is working alright except my output's not quite how I wanted it. For example, I input abcde, I should get edcba but this is what I get: edecdebcdea. as you can see it reverses it but still takes in all letters after the head (except for the original head):
    E De Cde Bcd de A. I'm new to recursion so I find this very confusing, any help would be appreciated. Thanks

    example output:
    Code:
    Enter a string:
    abcde
    x: abcde
    
    x: bcde
    
    x: cde
    
    x: de
    
    x: e
    
    x:
    
    mystring: edecdebcdea
    Here's the recursive function, I'm pretty sure the problem's somewhere in here:
    Code:
    strings reverse(strings x) 
        {
        cout<< "x: " <<x<<endl;                          //test output
        if (x.stringlength() <= 0)
    		{
    		return x;
    		}
    	return reverse(x.tail()) + x.head();        //recursive statement
        }
    relevant member functions from my class "strings"
    Code:
    /**********************************************************************
    	* Default Constructor:
    **********************************************************************/
    	strings::strings()					
    		{
    		int index = 0;
    		
    		while (index < 20)
    			{
    			strhead[index] = '\0';
    			strtail[index] = 't';
    			fullstring[index] = 'f';
    			index++;
    			}
    		head();
    		tail();
    		}
    		
    /**********************************************************************
    	* return string head
    **********************************************************************/
    	char *strings::head()
    		{
    		strncpy(strhead, fullstring, 1);
    		return strhead;
    		}
    	
    	/**********************************************************************
    	* Return string tail
    **********************************************************************/
    	strings strings::tail()
    		{
    		int index = 0;
    		strcpy(strtail, fullstring);
    		
    		//Test for null terminator
    		while (fullstring[index] != '\0')	
    			{
    			strtail[index] = fullstring[index + 1];
    			index++;
    			}
    		return strtail;
    		}
    	/**********************************************************************
    	*Return length of string 
    **********************************************************************/
    	int strings::stringlength()
    		{
    		return strlen(fullstring);
    		}
    	
    	/**********************************************************************
    	* Overloaded + operator
    	**********************************************************************/
    	strings strings::operator +(char right[1])
    		{
    		strcat(fullstring, right);
    		return fullstring;
    		}
    		
    /**********************************************************************
    	* Overloaded = operator
    	**********************************************************************/
    	void strings::operator =(const strings &right)
    		{
    		strcpy(fullstring, right.fullstring);
    		}

  2. #2
    Registered User
    Join Date
    Oct 2004
    Posts
    151
    I tested your reverse algorithm and it works fine. It is therefore likely that it's the rest of your class methods that have problems. (Many of them look downright nonsensical.)
    System: Debian Sid and FreeBSD 7.0. Both with GCC 4.3.

    Useful resources:
    comp.lang.c FAQ | C++ FQA Lite

  3. #3
    Registered User
    Join Date
    Oct 2006
    Location
    Canada
    Posts
    1,243
    heres one that i have written before:
    Code:
    string reverse(string str, int loopNum=0)
    {
    	if (loopNum >= str.length())
    		return str;
    	str.insert(loopNum, str.substr(str.length()-1,1));
    	str = str.substr(0,str.length()-1);
    	return reverse(str,++loopNum);
    }
    to call the function pass one parameter, such as: cout << reverse("joe");

  4. #4
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Why not just use this?
    Code:
    void print_reverse(const char *str) {
        if(*str) {
            print_reverse(str+1);
            std::cout << *str;
        }
    }
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

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. Custom String class gives problem with another prog.
    By I BLcK I in forum C++ Programming
    Replies: 1
    Last Post: 12-18-2006, 03:40 AM
  3. Compile Error that i dont understand
    By bobthebullet990 in forum C++ Programming
    Replies: 5
    Last Post: 05-05-2006, 09:19 AM
  4. Classes inheretance problem...
    By NANO in forum C++ Programming
    Replies: 12
    Last Post: 12-09-2002, 03:23 PM
  5. creating class, and linking files
    By JCK in forum C++ Programming
    Replies: 12
    Last Post: 12-08-2002, 02:45 PM