Thread: need some help..

  1. #1
    Registered User
    Join Date
    Feb 2008

    need some help..

    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~

    #include <iostream>
    using namespace std;
    int main(){
         string reversed;
         string str;
         cout<<"Input: ";
         for(int i = str.length() - 1; i >= 0; i--)
         reversed += str[i];
         cout<<"Output :"<<reversed<<endl;
    return 0;

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    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?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Rishon LeZion, Israel
    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
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    David J. Wheeler

Popular pages Recent additions subscribe to a feed