Thursday, April 2, 2026

Write a program to determine if the given string is a palindrome or not || C Lab Program

 WAP_E03: Write a C program to determine if the given string is a palindrome or not (Spelled same in both directions with or without a meaning like madam, civic, noon, abcba, etc.) || Strings


WAP_E03: C Lab Program


//to find the given string is a palindrome or not.
#include <stdio.h>
#include <string.h>

int main() {
    char str[20];
    int i,j;
    int flag = 0;
   
    printf("Enter a string: ");
    scanf("%s", str);
    // Calculate the string length, -1 for '\0'
    j=strlen(str)-1;
    // Compare characters from the start and end of the string
    // and stop if a mismatch is found or the middle of the string is reached.
    for(i=0;i<=j;i++,j--)
    {
        if(str[i]!=str[j])
        {
            flag=1;
            break;
        }
    }
    if (flag) {
        printf("%s is not a palindrome\n", str);
    } else {
        printf("%s is a palindrome\n", str);
    }
}



OUTPUT



Enter a string: madam
madam is a palindrome


Enter a string: abcdba
abcdba is not a palindrome


For all 2026 published articles list: click here

...till the next post, bye-bye & take care

No comments:

Post a Comment