"The best way to cheer yourself up is to try to cheer somebody else up." Mark Twain

Monday, March 1, 2010

WAP to check a string is palindrome or not

eg.

MADAM is palindrome
MAIDEN is not a palindrome.


#include<iostream.h>
#include<conio.h>
#include<string.h>
#include<stdio.h>

void main()
{

char text[50] , rev[50];

clrscr();

cout<< "\n Enter a string : ";
gets(text);

/* calculate the length of the string */
int len  = strlen(text);

/* reverse the string */

for(int i = len - 1, k = 0; i>=0 ; i--, k++)
{
rev[k] = text[i];
}

rev[k] = '\0';

if(strcmp(text, rev) == 0)
cout<< "\n The string " << text << " is a palindrome. ";
else
cout<< "\n The string " << text << " is not a palindrome. ";



getch();
}

No comments:

Post a Comment

C makes it easy to shoot yourself in the foot; C++ makes it harder, but when you do it blows your whole leg off.
Now Playing: Ballade Pour Adeline

About Me

My photo
I m an IT lecturer of a college. I love social-work. I want to do something beneficial for society before dying , that can promote our society, to some extent.