Skip to content
Laviyx Learn

Try

PPS Lab · Experiment 37

Check whether a string is a palindrome

CStringsJNTUH R22 · CS108ES

Aim

To write a C program that reads a word and checks whether it is a palindrome, that is, whether it reads the same from left to right and from right to left.

Algorithm

  1. Read the word.
  2. Set i to the first index (0) and j to the last index (length − 1).
  3. While i is less than j, compare the characters at i and j.
  4. If they are different, the word is not a palindrome. Stop comparing.
  5. Otherwise, increase i by 1 and decrease j by 1, and repeat.
  6. If all pairs matched, the word is a palindrome. Print the result.

Program

C
#include <stdio.h>
#include <string.h>

/* Returns 1 if str reads the same in both directions, otherwise 0 */
int is_palindrome(const char str[]) {
    int i = 0;
    int j = strlen(str) - 1;

    while (i < j) {
        if (str[i] != str[j]) {
            return 0;
        }
        i++;
        j--;
    }
    return 1;
}

int main(void) {
    char word[100];
    printf("Enter a string: ");
    if (scanf("%99s", word) != 1) {
        printf("Invalid input\n");
        return 1;
    }

    if (is_palindrome(word)) {
        printf("%s is a palindrome\n", word);
    } else {
        printf("%s is not a palindrome\n", word);
    }
    return 0;
}

Output

Odd length palindrome

Output 1
Enter a string: madam
madam is a palindrome

Even length palindrome

Output 2
Enter a string: noon
noon is a palindrome

Not a palindrome

Output 3
Enter a string: hello
hello is not a palindrome

Recorded by compiling and running this exact program. Typed input appears after each prompt.

How it works

The comparison is case-sensitive, so Madam is not a palindrome here because 'M' and 'm' are different characters. To ignore case, compare tolower(str[i]) with tolower(str[j]) (from ctype.h).

The program reads one word with scanf("%99s"), which is enough for words like madam, civic, noon and abcba. For a sentence with spaces, read it with fgets instead.

Common mistakes

  • Setting j to strlen(str) instead of strlen(str) - 1. Index strlen(str) holds the '\0', so the first comparison is always wrong.

  • Printing "palindrome" inside the loop after the first matching pair. You can only decide it is a palindrome after every pair has matched.

  • Comparing str[i] with str[len - 1 - i] for every i from 0 to len - 1. It still works, but it checks every pair twice; stopping when i meets j is enough.

  • Comparing strings with ==, as in word == reversed. That compares addresses, not contents. Use strcmp to compare two strings, or compare characters one by one as this program does.

Viva questions

Try to answer each one aloud before you open it.

  • What is a palindrome? Give some examples.

    A palindrome is a word, number or phrase that reads the same forwards and backwards, such as madam, civic, noon, level and 12321.

  • How many comparisons does the program make for a word of length n?

    At most n / 2 (rounded down), because each comparison checks one character from the front and one from the back.

  • In an odd-length word like madam, why is the middle letter never compared?

    When i and j both reach the middle letter, i < j is false and the loop stops. A letter always matches itself, so it does not need checking.

  • How else could you check for a palindrome using library functions?

    Copy the string, reverse the copy, and compare the two with strcmp. If strcmp returns 0, the string is a palindrome. (strrev is not part of standard C, so you may need to write the reverse loop yourself.)

  • Is the comparison case-sensitive?

    Yes. 'M' and 'm' have different ASCII codes (77 and 109), so Madam is reported as not a palindrome unless both sides are converted with tolower.

Learn the concepts

All Programming for Problem Solving Lab experiments →