Skip to lab content

UNIT 04 · Strings

Check whether a string is a palindrome

EXERCISE 04BC173 sample runs

THE SYLLABUS QUESTION

What you need to solve

Write a C program to determine if the given string is a palindrome or not (spelled the same in both directions, with or without a meaning, like madam, civic, noon, abcba).
Input format & conventions

One non-empty line of at most 255 bytes. Comparison is exact and case-sensitive; spaces and punctuation are included. Use ASCII text for this byte-based example.

UNDERSTAND THE IDEA

Explanation

Compare matching characters from the two ends and move inward. A mismatch means the string is not a palindrome.

Only half the characters need comparison. This version keeps the original text unchanged and does not ignore letter case or spaces.

PLAN BEFORE CODING

Algorithm

  1. Read a bounded, non-empty string.
  2. Start left at 0 and right at the string length.
  3. Decrement right before comparing the two characters.
  4. Stop on mismatch or after reaching the middle.
  5. Print the result.

SEE THE CONTROL FLOW

Flowchart

Follow the arrows from Start. Diamonds ask a question; labeled arrows show the answer. A returning arrow repeats a loop. Function internals are grouped where needed; later input checks follow the rules in the program.

Flowchart for Check whether a string is a palindrome: input, decisions, processing, output and loop paths

On a phone, scroll sideways to read the diagram at full size. Open full-size flowchart ↗

C17

Complete C program

Download .c
#include <stdio.h>
#include <string.h>

int readLine(char text[], size_t capacity) {
    if (fgets(text, (int)capacity, stdin) == NULL) return 0;
    size_t length = strlen(text);
    if (length > 0 && text[length - 1] == '\n') text[--length] = '\0';
    else if (!feof(stdin)) {
        int ch = getchar();
        if (ch != '\n' && ch != EOF) return 0;
    }
    if (length > 0 && text[length - 1] == '\r') text[length - 1] = '\0';
    return 1;
}

int main(void) {
    char text[256];
    if (!readLine(text, sizeof text) || text[0] == '\0') {
        puts("Invalid input."); return 1;
    }
    size_t left = 0, right = strlen(text);
    int palindrome = 1;
    while (left < right) {
        --right;
        if (text[left] != text[right]) { palindrome = 0; break; }
        ++left;
    }
    printf("Palindrome: %s\n", palindrome ? "yes" : "no");
    return 0;
}
Open in compiler ↗

Code loads into the existing compiler. Enter the sample input there; sign-in and execution rules stay the same.

Compile and run locally
gcc -std=c17 string-palindrome.c -o lab
./lab

On Windows, run .\lab.exe after compiling with GCC. The interest program requires the math library where applicable.

FOLLOW THE VALUES

Dry run

Step / stateOperationResult
Text = madamCompare positions 1 and 5m = m
Move inwardCompare positions 2 and 4a = a
Middle reachedNo mismatchPalindrome

CHECK THE BEHAVIOR

Sample input & output

Each output below was produced by compiling and running this exact program. Input values are entered in the stated order; the examples do not print input prompts.

Sample 1

INPUT
madam
OUTPUT
Palindrome: yes

Sample 2

INPUT
hello
OUTPUT
Palindrome: no

Sample 3

INPUT
Madam
OUTPUT
Palindrome: no

Common mistakes

  • Strip the input newline before comparison.
  • State whether comparison ignores spaces/case; this program does not.

WHY THIS GROWTH RATE?

Time and space complexity

Time O(L); O(L) input storage and O(1) additional working space.

Let L be the string length. Reading the string and finding its length take O(L). The two-pointer comparison examines at most floor(L / 2) pairs, doing constant work per pair.

O(L) + O(L / 2) simplifies to O(L) time. A mismatch may end the comparison early, but reading the complete input still costs O(L). The input string uses O(L) storage and only two indices and a flag are needed beyond it: O(1) auxiliary space.

Big-O describes how work grows as the stated input quantity grows; fixed factors and lower-order terms are omitted. The analysis treats fixed-width arithmetic as constant cost and the published limits as practical safety bounds.