Program 10 – Reverse It Function

Write a function reversit() which passes a string as a parameter and reverses the string using the procedure: First swap the first and the last character of the string, then the second and the second last character and so on.Write a program to exercise reversit(). The program should get a string from the user. Call reversit() and print out the result. Use an input method that allows embedded blanks. Test the program with Napoleon’s famous phrase. “Able was I ere I saw Elba”.null

#include<iostream>
#include<string.h>
using namespace std;


void reverseit(char *s, int i, int j)
{
    if(i<j)
    {
        char c;
        c = s[i];
        s[i] = s[j];
        s[j] = c;
        reverseit(s,i+1,j-1);
    }
    else{
        puts(s);
    }
    
}

int main()
{   
    char *s;
    cout<<"Enter string to reverse: ";
    gets(s);
    //cout<<strlen(s);
    reverseit(s, 0, strlen(s)-1 );
    return 0;
}

LEAVE A REPLY