Program to Reverse String C++

Write a Program to Reverse a String in C++.

Reversing a number is quite easy task, see this to reverse a number

Program to reverse a number

To reverse a String we just exchange the first character with last character till we reach the mid of the string.

Program to Reverse String C++:

#include<iostream>
using namespace std;

int main( )
{
    int l,i,j;
    char str[80];
    int temp;
    cout<<"Enter string :";
    gets(str);

    for(l=0;str[l]!='';l++);    //finding length of string

    for(i=0,j=l-1;i<l/2;i++,j--)
    {
        temp=str[i];
        str[i]=str[j];
        str[j]=temp;
    }

    cout<<"Reverse String isn"<<str;

    return 0;
}

 

OUTPUT:

reverse string c++

Leave a Reply