Thread: reverse string using recursive

  1. #1
    Registered User
    Join Date
    Oct 2011
    Posts
    4

    reverse string using recursive

    Code:
    #include <iostream>
    #include <string>
    using namespace std;
    
    void reverse(string, int);
    
    int main()
    {
        string s = "Example";
        
        cout << s<< endl;;
        
        reverse(s, 0);
    } // end main
    
    void reverse(string reverse, int i=0)
    {
        if (start == reverse.length())
            return;
        
        reverse(reverse,i+1);
        cout << reverse[i];
    }
    I understand most of this code but only thing i don't understand is the last 2 line.
    Code:
     reverse(reverse,i+1);
        cout << reverse[i];
    how does the index goes to the end of the array when i is 0?

  2. #2
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    First this code will not compile because of errors. The first error is you are trying to use the same name for your variable and your function. Second start is not defined.
    Code:
    void reverse(string toReverse, int i=0)
    {
        if (i == toRreverse.length())
            return;
         
        reverse(toReverse,i+1);
        cout << toReverse[i];
    }
    So after fixing those problems, what I think is confusing you is the following line:
    Code:
    void reverse(string toReverse, int i=0)
    The int i=0 in the above is called a default argument, this function can be called with one or two arguments. If you call the function with one argument then it will use zero for the second argument. If you call the function with two arguments then the passed argument will be used instead of the zero. See this link for a more detailed description: Functions II.

    Jim

  3. #3
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. reverse a string without string.h library
    By antros48 in forum C Programming
    Replies: 6
    Last Post: 09-10-2011, 06:01 PM
  2. Reverse a string
    By GeorgeOfTheBush in forum C Programming
    Replies: 1
    Last Post: 12-26-2009, 01:24 PM
  3. reverse string
    By St0rM-MaN in forum C Programming
    Replies: 11
    Last Post: 06-15-2007, 01:40 PM
  4. Reverse a string (without using any string functions?)
    By geetard in forum C Programming
    Replies: 2
    Last Post: 11-15-2006, 07:42 PM
  5. string in reverse
    By Teele777 in forum C Programming
    Replies: 2
    Last Post: 04-18-2002, 07:27 PM