Sunday, 7 September 2014

Reverse a string without modifying original string

Reverse a string without editing original string and no extra space and swap functions
Algorithm
  1. Use recursion to keep moving till end of string.
  2. Display data in each index of string 
Input    : string s  (Eg : rev me )
Output : Reverse of s (Eg: em ver )


Implementation

#include <iostream>
#include <string>
using namespace std;
void Reverse ( const char * s )
{
    if ( *s )
    {
        Reverse ( s+1 ) ;
        cout<<*s;
    }
}
int main()
{
    string s;
    getline ( cin,s );
    const char *p= s.c_str();
    Reverse ( p );
    getchar();

}
Please comment if you find anything incorrect.

No comments:

Post a Comment