-
need some help..
hye..
i need some help in recursion..anyone can help me to transform this coding into recursion??
please teach me how to transform it..thank u~
insert
Code:
#include <iostream>
using namespace std;
#include<conio.h>
#include<string.h>
int main(){
string reversed;
string str;
cout<<"Input: ";
cin>>str;
for(int i = str.length() - 1; i >= 0; i--)
{
reversed += str[i];
}
cout<<"Output :"<<reversed<<endl;
getch();
return 0;
}
-
Start with the base case. If you have a string of zero or one character, how would you reverse it?
Then take the next possibility: if you have a string of two characters, how would you reverse it?
-
You may want ot fix the current code first:
conio.h is not standard
getch is not standard - read FAQ how to prevent the program window from dissapearing
<string.h> is a C-header for C-string manipulation routines, C++ versin is <cstring>
You do not need any of this - use <string> for std::string class
using namespace std;
should not be placed before any header - it can cuase problems
to make your code recursive write a fuction reverseString(string& s)
that will take last char of the string
and return
lastChar + reverseString(substring without lastChar)
Stop constantly deleting your post... Better rename it to something valueable