c++ - Recursive method of reversing string -


i trying write recursive method of reversing string 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 output not correct. "after="<<ch printing last character of string. argument of function s std::string, index starting 0, , l length of string. can 1 point out doing wrong thing.

you might have figured out problem.

another approach, if don't like/want iterator or reverse function.

 string revstr(string str){         if (str.length() <= 1) {             return str;         }else{             return revstr(str.substr(1,str.length()-1)) + str.at(0);         }     } 

Comments

Popular posts from this blog

javascript - DIV "hiding" when changing dropdown value -

Does Firefox offer AppleScript support to get URL of windows? -

android - How to install packaged app on Firefox for mobile? -