Saturday, January 29, 2011

Reverse a string in c++

Here is a program to reverse a string , quite interesting and easy

******************* Start of Program ************************


// Reversing a string

#include"iostream.h"
#include"conio.h"
#include"stdio.h"

void main()
{
clrscr();
char str1[50],str2[50];
cout<<"Enter a String :: ";
gets(str1);

int h=0;
while(str1[h]!='\0') // to get number of chracters in sub string
{
str2[h]=str1[h];
h++;
}
str2[h]='\0';

int k=0,m=h-1;

while(m>=0)
{
str1[k]=str2[m];
m--;
k++;
}


cout<<"\n\nOutput string is :: "<
getch();
}

******************* END ************************