C PROGRAM FOR STRING PALINDROME

   

Consider the following I/O samples.


SAMPLE INPUT: radar

SAMPLE OUTPUT: Palindrome


The algorithm used here is :

    1. Input string.
    2. Reverse the string.
    3. Check the reversed string and the original string.
    4. If the strings are same, it is a Palindrome.
    5. Print the output.

C PROGRAM:  

#include <stdio.h>

#include <string.h>

int main()

{

    char string1[20];

    int i, length;

    int flag = 0;

    printf("Enter a string:");

        scanf("%s", string1);

    length = strlen(string1);

    for(i=0;i < length ;i++)

    {

        if(string1[i] != string1[length-i-1])

        {

            flag = 1;

            break;

       }

    }

    if (flag) 

        printf("%s is not a palindrome", string1);

    else 

        printf("%s is a palindrome", string1);

    return 0;

}


OUTPUT:





The given source code in C program "for String Palindrome" is short and simple to understand. The source code is well tested in DEV-C++ and is completely error free. 

If you have any feedback about this article and want to improve this, please comment in the comment section.



Comments

Popular posts from this blog

INVERTED PYRAMID STAR PATTERN

C PROGRAM TO PRINT ALL LEAP YEARS IN A GIVEN RANGE

C PROGRAM TO PRINT THE REMAINING DAYS AND COMPLETED DAYS FROM A GIVEN DATE