# To check whether the given string is palindrome-method 1

Here is  the program to check if the entered string is a palindrome or not. As we have seen in the earlier cases, Palindrome is a string segment, which reads same from both the directions. The same method is implemented here in the program also.

Logic :  The given string is reversed using a method, which puts the reversed string in the resultant string from the EOL (End Of Line). After reversing, it compares if strings are one and the same. If so, sets a flag, “pal” as true (i.e. 1), and hence prints out the result.

The problem can also solved in a bit sophisticated method, where we check  without reversing the string, but directly.

#include<stdio.h>
#include<conio.h>
int stpal(char str1[50], char str2[50]);
void main()
{
char str[50], rev[50];
int pal;
clrscr();
printf(“\n\n\t ENTER A STRING…: “);
gets(str);
pal = stpal(str, rev);
printf(“\n\t THE REVERSED STRING IS…: “);
puts(rev);
if(pal)
printf(“\n\t THE ENTERED STRING IS A PALINDROME”);
else
printf(“\n\t THE ENTERED STRING IS NOT A PALINDROME”);
getch();
}
int stpal(char str1[50], char str2[50])
{
int i = 0, len = 0, r = 0, pal = 1;
while(str1[len]!=’\0′)
len++;
for(i=len-1; i>=0; i–)
{
str2[r] = str1[i];
r++;
}
str2[r] = ‘\0’;
for(i=0; i<=len-1; i++)
{
if(str1[i] == str2[i])
pal = 1;
else
{
pal = 0;
break;
}
}
return pal;
}

Start exploring endless computing possibilities with your own Raspberry Pi computer and accessories. Perfect for beginners and students.

### 2 Responses to “To check whether the given string is palindrome-method 1”

1. Hey, I would like to share my version of the palindrome function too. Kindly point out any mistakes.

void palindrome(char *s)
{
char*p=s;
while (*p++ !=”)
;

while ( p– > s++ )
{

if( *p == *s)
;

else
{
printf(“String is not a palindrome”);
break;
}
}

2. Forgive a little mistake

int palindrome(char *s)
{
char*p=s;
while (*p++ !=”)
;

while ( p– > s++ )
{

if( *p == *s)
continue;

else
return 0;

}
return 1;
}