[c++] Write a recursive function takes a word string as input argument and reverse the word. Print out the results of recursive calls. Also write a main program accepts input from the user and then call the recursive function. Example: Enter a word: hello h eh leh lleh olleh
#include<iostream> using namespace std; void reverseInput(string x) { cout << x << endl; if (x>word.length(1)) { reverseInput(word.length(x)-word.length((0)); } } int main() { string word; cout << "Enter a word: "; cin >> word; cout << reverseInput(word) << endl; return 0; } I'm not sure how to write the actual recursive part using strings
I did it recursively, but I still believe this is a horrible way to reverse a string. There's probably a library function to accomplish this, and if there isn't then I'm sure an iterative procedure would do the job better.
I just can't figure out how to output the 'recursive calls' as in the example. http://ideone.com/oxrCv http://ideone.com/ewIsg#li_ewIsg
maybe adhokshaj knows :-D
maybe it's because I did it backwards :-P
It can be done in one line in Python, with no fancy pants recursion :-P http://ideone.com/19Hse
It can be done in most languages without recursion, and in many languages that's more efficient. Notably, in functional languages that optimize tail calls, it can be done equally efficiently recursively. And it's worth it to learn how to think recursively. It's very different, but it can be a beautiful way to approach problems.
//initialize function test(); private function test():void{ trace("this function will call itself recursively"); test(); }
recursive funtions tend to be faster than for loops which can be a problem becasue if you have other functions that are slower but provide data to a recursive function you might find yourself with unexpected data or an errror.......this is unusual but within reality I think.
#include <iostream> #include <string> using namespace std; void reverseString( string text ) { if( text.length() == 0 ) return; string character = text.substr( 0, 1 ); reverseString( text.substr( 1 ) ); cout << character; } int main( int argc, char *argv[] ) { reverseString( "One if by land." ); cout << endl; return 0; }
^Does not return recursive calls :/
I'm sure shadowfiend could do easily do this :-D
Sorry about that. All that would need to done is to pass down an empty string, append character to it, and cout it instead of character.
Hey, I have a question.
int main(int argc, char** argv).... what are you pointing to? Why are you using arguments? Thanks!!
int main(int argc, char** argv) (or more often you might find it as int main(int argc, char *argv[]) but it's basically the same thing) is just a convention I use, and the program will work just as right if you just leave the parameters of main empty like int main(). Writing argc and argv lets your program access the arguments you pass it when you call it using the terminal: argc is the number of arguments you pass it, which is 1 by default if you simply call the program name, and argv[] is the argument vector, and it's zeroth element is the file path of your program, and any subsequent elements in argv depend on whether you passed it arguments. Your code does not really use argc or argv, so if I left it out like this: http://ideone.com/AZjsi it will still work the same way.
Though it's worth mentioning that the two parameters, and the int return type, are theoretically the only way to write valid C++. C doesn't care, but C++ I believe requires those parameters and that return type (compilers generally don't choke if you don't have them, though).
Join our real-time social learning platform and learn together with your friends!