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
    21,438
    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?
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,473
    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
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

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