{ }codelper
reversing stringsif / elseP0004

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"
Example 1 · stdin
level
stdout
yes
Example 2 · stdin
code
stdout
no
main.py
Input (stdin)

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.

In other languagesShow solutions by language · 8 languages

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.

Pythonmain.py · 2 lines
s = input().strip()print("yes" if s == s[::-1] else "no")
JavaScriptmain.js · 2 lines
const s = require("fs").readFileSync(0, "utf8").trim();console.log(s === [...s].reverse().join("") ? "yes" : "no");
TypeScriptmain.ts · 3 lines
import { readFileSync } from "node:fs";const s: string = readFileSync(0, "utf8").trim();console.log(s === [...s].reverse().join("") ? "yes" : "no");
JavaMain.java · 9 lines
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");    }}
C#Main.cs · 12 lines
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");    }}
C++main.cpp · 8 lines
#include <iostream>#include <string> int main() {    std::string s;    std::cin >> s;    std::cout << (std::string(s.rbegin(), s.rend()) == s ? "yes" : "no") << '\n';}
Cmain.c · 12 lines
#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;}
Craykonmain.cray · 9 lines
s = Console.ReadLine();if (s.Reverse() == s){    Console.WriteLine("yes");}else{    Console.WriteLine("no");}