C++ Program

Object:-C++ Program to Reverse a String

#include<iostream.h>
#include<stdio.h>
#include<string.h>
 
int main()
{
    char a[50],temp;
    int i,j,len;
    cout<<"Enter any string:
";
    gets(a);
    len=strlen(a);
 
    cout<<"Reverse of the string is: ";
 
    for(i=0,j=len-1;i<len/2;++i,--j)
    {
        temp=a[i];
        a[i]=a[j];
        a[j]=temp;
    }
 
    cout<<a;
    return 0;
}

Output

Enter any String:
C++ Program
Reverse of the string is: margorp ++C

Explanation

The user is asked to enter a string and then the reverse of the string is displayed.







You may also like: