Read one word and print yes if it reads the same forwards and backwards (ignoring upper and lower case), otherwise no.
What to build
A comparison of a word with itself, from both ends.
Requirements
- Read the word.
- Print
yesif it reads the same in both directions, ignoring case.
Input
One word of 1 to 50 letters.
Output
yes or no.
Constraints
The word has from 1 to 50 letters.
Example
Input:
racecar
Output:
yes
Why it works
Only half the word needs comparing, and tolower makes A and a count as the same letter.
Examples
Example 1 · Input
racecar
Expected output
yes
Example 2 · Input
rotation
Expected output
no
Hints
Hint 1
Compare s[i] with s[n - 1 - i] for i up to n / 2.
Hint 2
Use tolower so that Level counts as a palindrome.
Solution
Show one correct solution (try it yourself first)
#include <stdio.h>
#include <string.h>
#include <ctype.h>
int main(void) {
char w[64];
scanf("%63s", w);
int n = strlen(w);
int ok = 1;
for (int i = 0; i < n / 2; i++)
if (tolower(w[i]) != tolower(w[n - 1 - i])) ok = 0;
printf("%s\n", ok ? "yes" : "no");
return 0;
}