The recursive function is only reversing the first and last characters of a string. I've been trying for hours but cant find the error. Any help would be awesome. The code:

#include <iostream>
#include <string>
using namespace std;

void str_reverse(char str[], int length)
{
	
    char temp, temp1;
    int starti=0;
    int endi = length-starti;
    temp1 = str[endi-1];
    temp = str[starti];
    
    if(starti>=length-starti)
    {
         return;
    }
    str[starti] = temp1;
    str[endi-1] = temp;
    starti++;
    return (str_reverse(str, length-1));
}

int main()
{
    string test= "lighter";
    str_reverse(test, 7);
    cout << "output= " << test;
    return 0;
}

The recursive function is only reversing the first and last characters of a string. I've been trying for hours but cant find the error. Any help would be awesome. The code:

#include <iostream>
#include <string>
using namespace std;

void str_reverse(char str[], int length)
{
	
    char temp, temp1;
    int starti=0;
    int endi = length-starti;
    temp1 = str[endi-1];
    temp = str[starti];
    
    if(starti>=length-starti)
    {
         return;
    }
    str[starti] = temp1;
    str[endi-1] = temp;
    starti++;
    return (str_reverse(str, length-1));
}

int main()
{
    string test= "lighter";
    str_reverse(test, 7);
    cout << "output= " << test;
    return 0;
}

A few problems. One, look at this call and this function:

string test= "lighter";
    str_reverse(test, 7);

    void str_reverse(char str[], int length)

It gave me a compile error until I changed test to a character array to match the function.

Two:

void str_reverse(char str[], int length)

return (str_reverse(str, length-1));

Not an error, or at least my compiler let me get away with it, but you are returning something from a void function here. Just call it. As to the rest of your problem, look carefully at the two arguments in this line and make sure they are what you want.:

return (str_reverse(str, length-1));

The first part was just a stub. I had the wrong string in my mind. I'm still not seeing whats wrong. Could you explain a little farther.

The first part was just a stub. I had the wrong string in my mind. I'm still not seeing whats wrong. Could you explain a little farther.

Post the exact code that compiles and gives you the problem, along with your results, whether it is working or not. To help you debug, place the following line:

cout << str << endl;

before the recursive call so you can see each iteration. This may help you see what is occurring.

A lot of the code in your function is superfluous. Try simplifying it to what you actually need. What is the point of starti? it's always read as 0. starti is a local non-static variable, so your starti++; at the end of the function does nothing useful.

Also, in the recursive call to reverse, length-1 should be length-2, as you've just swapped the first AND last characters (that's 2 characters). You'll also want to increment the pointer for each call so that you don't keep swapping the first character with every other character.

void reverse(char *str, int len){
   if(len >= 2){
      swap(str[0], str[len-1]);  // swap first & last chars
      
      reverse(&str[1], len - 2); // swap the rest - (but not the chars we just swapped)
      }
   }

Thanks for the help everyone. My recursive function is working!!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.