Hello again :),
I am writing a code that will recursively write a string in reverse. The problem is in my recursiveReverse function. I feel like the idea is right, but the implementation is incorrect. My thought process was that I could start the loops at the beginning and the end of the string and then swap the two characters. Then, I could increase the iteration of the beginning by one and decrease the iteration of the end by one, and the repeat the swap. If you guys have any suggestions or advice I would really appreciate it. Thanks again ahead of time.
#include <iostream>
#include <string>
using namespace std;
void recursiveReverse( char a[], int start, int end )
{
int i, j;
for(i=start; i<end; i++)
{
for(j=end;j>start;j--)
{
char temp = a[i];
a[i]=a[j];
a[j]=temp;
}
}
}
void stringReverse( char str[] )
{
int length = strlen( str );
recursiveReverse( str, 0, length - 1 );
}
int main()
{
char a[20];
for (int i = 0; i < 20; i++ )
a[i] = 'A' + i;
a[19] = '\0';
cout << "string before reversing: " << a << endl;
stringReverse( a );
cout << "string after reversing: " << a << endl;
return 0;
}