Index « Previous Next »

Question

A palindrome is a string that reads the same backward as forward. For example, the words dad, madam and radar are all palindromes. Write a programs that determines whether the string is a palindrome.

Source Code

#include <stdio.h>

int main()
{
    char str[80];
    int i, l;

    printf("Enter string: ");
    gets(str);

    /* finding length of string */
    for (l = 0; str[l] != '\0'; l++);

    /* Comparing first element with last element till middle of string */
    for (i = 0; (i < l / 2) && (str[i] == str[l - i - 1]); i++);

    if (i == l / 2)
    {
        printf("Palindrome");
    }
    else
    {
        printf("Not a palindrome");
    }

    return 0;
}

Output

Enter string: madam
Palindrome