need some help..

This is a discussion on need some help.. within the C++ Programming forums, part of the General Programming Boards category; hye.. i need some help in recursion..anyone can help me to transform this coding into recursion?? please teach me how ...

  1. #1
    Registered User
    Join Date
    Feb 2008
    Posts
    4

    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;
    }

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    23,618
    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
    "I Win!" by U. Lose vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,754
    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
    To be or not to be == true

Popular pages Recent additions subscribe to a feed

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21