Basic

Switch Case

Goto

Operators

if Statement

Nested if

While Loop

For Loop

Patterns

Array

2D Array

String Function Example

Pointers

Recursion Function

Structure

Excersises

Others


C Program to Check whether the Given String is a Palindrome


Write a c program to check whether the given string is a palindrome or not.

  • Reads characters one by one into str until a newline (\n) is encountered.
  • Stores the total length of the string entered using index i.
  • Compares characters from both ends moving toward the center to check for palindrome.
  • Prints whether the string is a palindrome based on the comparison result.
Example : pgm.c
#include <stdio.h>

int main() {
    char str[100];
    int i = 0, j, isPalindrome = 1;

    printf("Enter a string: ");

    // Read input manually (character by character)
    while ((str[i] = getchar()) != '\n') {
        i++;
    }

    // Check if the string is a palindrome
    for (j = 0; j < i / 2; j++) {
        if (str[j] != str[i - j - 1]) {
            isPalindrome = 0;
            break;
        }
    }

    if (isPalindrome)
        printf("The string is a palindrome.\n");
    else
        printf("The string is not a palindrome.\n");

    return 0;
}

Output 1:

Enter a string: level
The string is a palindrome.

Output 2:

Enter a string: computer
The string is not a palindrome.