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
- Read the word.
- Set i to the first index (0) and j to the last index (length − 1).
- While i is less than j, compare the characters at i and j.
- If they are different, the word is not a palindrome. Stop comparing.
- Otherwise, increase i by 1 and decrease j by 1, and repeat.
- If all pairs matched, the word is a palindrome. Print the result.
Program
#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
Enter a string: madam
madam is a palindromeEven length palindrome
Enter a string: noon
noon is a palindromeNot a palindrome
Enter a string: hello
hello is not a palindromeRecorded 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 ofstrlen(str) - 1. Indexstrlen(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]withstr[len - 1 - i]for everyifrom 0 tolen - 1. It still works, but it checks every pair twice; stopping whenimeetsjis enough.Comparing strings with
==, as inword == reversed. That compares addresses, not contents. Usestrcmpto 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
iandjboth reach the middle letter,i < jis 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. Ifstrcmpreturns 0, the string is a palindrome. (strrevis 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), soMadamis reported as not a palindrome unless both sides are converted withtolower.