codescracker


c++

C++ Program to Reverse String



« Previous Program Next Program »


Reverse String in C++

To reverse a string in C++ programming, then ask to the user to enter a string, now make a variable say temp of char type and start swapping. Place first character of the string in temp and last character of the string in the first, then place temp in the last and continue, to swap string as shown in the following program.

C++ Programming Code to Reverse String

Following C++ program ask to the user to enter a string to reverse it and display the reversed string on the screen:

/* C++ Program - Reverse String */
		
#include<iostream.h>
#include<conio.h>
#include<stdio.h>
void main()
{
	clrscr();
	char str[100], temp;
	int i=0, j;
	cout<<"Enter the String : ";
	gets(str);
	i=0;
	j=strlen(str)-1;
	while(i<j)
	{
		temp=str[i];
		str[i]=str[j];
		str[j]=temp;
		i++;
		j--;
	}
	cout<<"Reverse of the String = "<<str;
	getch();
}

When the above C++ program is compile and executed, it will produce the following result:

C++ program to reverse string

Same Program in Other Language

You may like the same program in other programming languages:


« Previous Program Next Program »



Tools
Calculator

Quick Links
Signup - Login - Give Online Test