To check whether the given string is palindrome-method 2

Thursday, December 4th, 2008

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.

Author Name :
Ranjith

Total : 13 Comments


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

  1. Bird says:

    It works fine but it is case sensitive. How to solve that?

  2. Chetan says:

    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();

    }

  3. lopa says:

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

  4. noel says:

    i couldn’t understand the following part:
    for(j=0;str[j]!=” “;j++)
    j=j–;
    i=0

  5. priya says:

    last program is working.easy to enderstand

  6. SUMI says:

    9TH LINE IS USED FOR CALCULATING THE LENGTH OF THE STRING..

  7. m says:

    Is it raptor compatible ???

  8. Mahesh kumar dewat says:

    #include
    #include
    #include
    void main(void)
    {
    int i,len,halflen,flag=1;
    char str[50];
    clrscr();
    printf(“Enter a string:\n”);
    gets(str);
    len=strlen(str);
    halflen=len/2;
    for(i=0;i<halflen;i++)
    {
    if(str[i]!=str[i+halflen])
    flag=0;
    break;

    }
    if(flag)
    printf("It is a Palindrome.");
    else
    printf("It is not a Palindrome.");
    getch();
    }

  9. Mahesh kumar dewat says:

    #include
    main()
    {
    char a[20],b[20];
    printf(“enter a string”);
    scanf(“%s”,&a);
    strcpy(b,a);//copies string a to b
    strrev(b);//reverses string b
    if(strcmp(a,b)==0)//compares if the original and reverse strings are same
    printf(“\n%s is a palindrome”,a);
    else
    printf(“\n%s is not a palindrome”,a);
    return 0;
    }

  10. pradeep says:

    fuck u man u r wrong…………….both eg are wrong.

  11. deena says:

    definitely this will work…..thanks to mahesh kumar dewat.

    #include
    #include//this headel file is important
    main()
    {
    char a[20];
    char b[20];
    printf(“enter a string”);
    gets(a);
    strcpy(b,a);//copies string a to b
    strrev(b);//reverses string b
    if(strcmp(a,b)==0)//compares if the original and reverse strings are same
    printf(“\n%s is a palindrome”,a);
    else
    printf(“\n%s is not a palindrome”,a);
    return 0;
    }

  12. Kaushik says:

    You need to include the header file to get the desired output!#include
    #include
    #include
    main()
    {
    char a[20];
    char b[20];
    printf(“enter a string”);
    gets(a);
    strcpy(b,a);//copies string a to b
    strrev(b);//reverses string b
    if(strcmp(a,b)==0)//compares if the original and reverse strings are same
    printf(“\n%s is a palindrome”,a);
    else
    printf(“\n%s is not a palindrome”,a);
    return 0;
    }

  13. Ranjeet says:

    @ Kaushik

    #include
    #include

Leave a Reply

Question and Answer
C/C++ Unix & Linux Wordpress
Source codes
C C++ Java

Free email signup

Email: