Write a member function called reverseit() that reverses a string ( an array of character). Use a for loop that swaps the first and last characters, then the second and next-to last characters and so on. The string should be passed to reverseit() as an argument.

This answer is restricted. Please login to view the answer of this question.

Login Now
#include<iostream>
#include<string.h>

using namespace std;

void reverseit(char *p){

    int j;

    int len=strlen(p);

    j=len-1;

    for(int i = 0; i < len / 2; i++){

        char a=p[i];
        p[i]=p[j];
        p[j]=a;
        j--;

    }

    cout<<"\nReverse Of String :";

    puts(p);

}

int main(){

    char str[50];

    char ch;

    cout<<"\nEnter The String : ";

    gets(str);

    reverseit(str);
    
    return 0;

}

The output of above program is

Enter The String : Suresh

Reverse Of String :hseruS
If you found any type of error on the answer then please mention on the comment or report an answer or submit your new answer.
Leave your Answer:

Click here to submit your answer.

Discussion
0 Comments
  Loading . . .