C Program to Check String Palindrome Without Using String Handling Function

Question: Write a program in C to check whether a given string is PALINDROME or NOT without using string handling function.


#include<stdio.h>

int main()
{
    char string[40];
    int length=0, flag=1,i;

    printf("Enter string:\n");
    gets(string);

    for(i=0;string[i]!='\0';i++)
    {
        length++;
    }

    for(i=0;i< length/2;i++)
    {
        if( string[i] != string[length-1-i] )
        {
            flag=0;
            break;
        }
    }

    if(flag==1)
    {
        printf("PALINDROME");
    }
    else
    {
        printf("NOT PALINDROME");
    }
    return 0;
}

Output

Run 1:
------------
Enter String:
madam ↲
PALINDROME

Run 2:
------------
Enter String:
step on no pets ↲
PALINDROME

Run 3:
------------
Enter String:
madam ↲
codesansar 
NOT PALINDROME

Note: ↲ represents ENTER key is pressed.