Palindrome check
A word of lowercase English letters is given.
Print "yes" if it reads the same forwards and backwards (a palindrome), otherwise "no".
- Input
- word length 1 to 100, lowercase letters only
- Output
- "yes" or "no"
level
yes
code
no
A word of lowercase English letters is given.
Print "yes" if it reads the same forwards and backwards (a palindrome), otherwise "no".
level
yes
code
no
When you run, these lines are fed to input()/stdin in order (the program doesn't stop to ask).
$ Ready
Open this on a computer to write and run code. On mobile you can read the problem and the solutions.
Reversing in one line: Python s[::-1], Java new StringBuilder(s).reverse(), C++ string(s.rbegin(), s.rend()).
In C the natural way is a loop comparing from both ends toward the middle.
s = input().strip()print("yes" if s == s[::-1] else "no")const s = require("fs").readFileSync(0, "utf8").trim();console.log(s === [...s].reverse().join("") ? "yes" : "no");import { readFileSync } from "node:fs";const s: string = readFileSync(0, "utf8").trim();console.log(s === [...s].reverse().join("") ? "yes" : "no");import java.util.Scanner; public class Main { public static void main(String[] args) { String s = new Scanner(System.in).next(); String r = new StringBuilder(s).reverse().toString(); System.out.println(r.equals(s) ? "yes" : "no"); }}using System; class Program{ static void Main() { string s = Console.ReadLine().Trim(); char[] r = s.ToCharArray(); Array.Reverse(r); Console.WriteLine(new string(r) == s ? "yes" : "no"); }}#include <iostream>#include <string> int main() { std::string s; std::cin >> s; std::cout << (std::string(s.rbegin(), s.rend()) == s ? "yes" : "no") << '\n';}#include <stdio.h>#include <string.h> int main(void) { char s[101]; scanf("%100s", s); int n = strlen(s), ok = 1; for (int i = 0; i < n / 2; i++) if (s[i] != s[n - 1 - i]) ok = 0; puts(ok ? "yes" : "no"); return 0;}s = Console.ReadLine();if (s.Reverse() == s){ Console.WriteLine("yes");}else{ Console.WriteLine("no");}