Friday, January 29, 2010

Checking the string if its a Palindrome- Program

#include < iostream >
#include < string >
using namespace std; // the namespace for cout<< & such functions

int main()
{
char strn[80];
cout<<"Enter the string: ";
cin.getline(strn,80);
int len=strlen(strn);

bool flag=true; // create a Boolean value, "flag" to be used in our loop

for(int c=0;c!=len/2;c++) // do a loop from 0 to half the length of the string
{
if(flag) // if it is a palindrome so far
{
if(strn[c]!=strn[len-c-1]) // check the characters match
{
flag=false; // if they don't set the indicator to false
}

}
else
{
break; // if it is not a palindrome, exit the for loop
}
}

// if flag is true cout "Palindrome" otherwise output "Not Palindrome"

if(flag)
{
cout<<"Palindrome";
}
else
{
cout<<"Not Palindrome";
}

cin.get();
return 0;
}

No comments:

Post a Comment