C program to check whether a given string is a palindrome
kw.c
#include <stdio.h>
#include <string.h>
int main()
{
    char s[50], reverse[50], temp;
    printf("———————————————————————————————————————————");
    printf("\nProgram to check if a string is palindrome");
    printf("\n———————————————————————————————————————————");
    printf("\nEnter the string ");
    scanf("%s", s);
    strcpy(reverse, s);
    for (int i = 0, j = strlen(s) - 1; i < j; i++, j--)
    {
        temp = s[i];
        s[i] = s[j];
        s[j] = temp;
    }
    if (strcmp(s, reverse) == 0)
    {
        printf("String is palindrome");
    }
    else
    {
        printf("String is not palindrome");
    }
    printf("\n———————————————————————————————————————————\n");
    return 0;
}
Output
kodingwindow@kw:~$ gcc kw.c
kodingwindow@kw:~$ ./a.out ——————————————————————————————————————————— Program to check if a string is palindrome ——————————————————————————————————————————— Enter the string MOM String is palindrome ——————————————————————————————————————————— kodingwindow@kw:~$
What Next?
C Arrays
Advertisement