I am trying to write a recursive method of reversing the string as below.
void reverse(string s,int i,int l)
{
static int j;
while(i<l)
{
char ch=s[i];
cout<<ch<<endl;
reverse(s,i+1,l);
cout<<"after="<<ch<<endl;
s[j]=ch;
j++;
}
cout<<s<<endl;
s[j]=0;
}
But my output is not correct. "after="<<ch
is always printing last character of the string.
Argument of the function is s is std::string, i is index starting from 0, and l is the length of the string.
can any one point out where I am doing wrong thing.