Write a C++ program to reverse a string.

Program Code:

#include<iostream>
using namespace std;
int main( )
{
int i, length; //variable to hold length of string
int temp;
char name[30];
cout<<"Enter your string to reverse : ";
cin.getline(name, 30);
cout<<endl;
for(i = 0; name[i] != '\0'; i++)   // loop for finding length of string
{
length = length + 1;
}   
for(int i = 0, j = length - 1; i < length/2; i++, j--)
{
temp = name[i];
name[i] = name[j];
name[j] = temp;
 }
cout << "Reversed string is equal to : " << name; 
cout<< endl;
return 0;
}

The program output is tested on www.jdoodle.com

Output:
Enter your string to reverse : mukesh
Reversed string is equal to : hsekum

Thanks
Mukesh Rajput

Mukesh Rajput

Mukesh Rajput

I am a Computer Engineer, a small amount of the programming tips as it’s my hobby, I love to travel and meet people so little about travel, a fashion lover and love to eat food, I am investing a good time to keep the body fit so little about fitness also..

Post A Comment:

0 comments: