To check whether the given string is palindrome-method 2

Here is  another program, advanced version of the previous program, to check if the entered string is a palindrome. 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 referred by two names, where one is traced by the SOL (Start Of Line) and the other is traced from EOL (End Of Line). In every iteration, the condition is checked, if both the characters are 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 another method, where we check  by reversing the string and then comparing.

#include<stdio.h>
#include<conio.h>
int stpal(char str[50]);
void main()
{
char str[50];
int pal;
clrscr();
printf(“\n\n\t ENTER A STRING…: “);
gets(str);
pal = stpal(str);
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 str[50])
{
int i = 0, len = 0, pal = 1;
while(str[len]!=’\0′)
len++;
len–;
for(i=0; i<len/2; i++)
{
if(str[i] == str[len-i])
pal = 1;
else
{
pal = 0;
break;
}
}
return pal;
}
Download exe and source code here.

Editorial Team
Editorial Team

We are a group of young techies trying to provide the best study material for all Electronic and Computer science students. We are publishing Microcontroller projects, Basic Electronics, Digital Electronics, Computer projects and also c/c++, java programs.

13 thoughts on “To check whether the given string is palindrome-method 2

  1. THIS THING ALSO WORKS !! EASILY UNDERSTANDABLE !!!

    #include
    #include
    int main()
    {
    int i,j,check=0;
    char str[100];

    printf(“ENTER A STRING:\n”);
    gets(str);
    for(j=0;str[j]!=”;j++);
    j=j-1;
    i=0;
    while(i<=j)
    {
    if(str[i]==str[j])
    {
    i=i+1;
    j=j-1;
    check=1;
    }
    else
    {
    check=0;
    break;
    }
    }

    if(check==1)
    printf("PALINDROME:");
    else
    printf("NOT A PALINDROME\n");

    getch();

    }

  2. i couldnt understand ur 9th number line// for(j=0;str[j]!=”;j++);
    can u pls explain?

Leave a Reply

Your email address will not be published. Required fields are marked *

Get the latest updates on your inbox

Be the first to receive the latest updates from Codesdoc by signing up to our email subscription.

    StudentProjects.in