Write a java program that determines whether the text the user inputs is a palindrome or not | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 2

Write a java program that determines whether the text the user inputs is a palindrome or not

A palindrome is a piece of text that can be read the same way in either direction

16th Mar 2017, 9:10 AM
Ahmed Sameh
Ahmed Sameh - avatar
4 Answers
+ 1
public static boolean isPalindrome(String text) { //if you want case-insensitivity: // text = text.toLowerCase(); int l = text.length(); int to = l / 2; for (int i = 0; i < to; ++i) if (text.charAt(i) != text.charAt(l - 1 - i)) return false; return true; }
16th Mar 2017, 9:37 AM
Magyar Dávid
Magyar Dávid - avatar
0
import java.util.*; class Palindrome { public static void main(String args[]) { String original, reverse = ""; //Get input from user Scanner in = new Scanner(System.in); System.out.println("Enter a string to check if it is a palindrome"); original = in.nextLine(); int length = original.length(); //For every character in original word check its same in reverse for ( int i = length - 1; i >= 0; i-- ) reverse = reverse + original.charAt(i); //To check whether both original and reverse are equal if (original.equals(reverse)) System.out.println("Entered string is a palindrome."); else System.out.println("Entered string is not a palindrome."); } }
16th Mar 2017, 9:35 AM
Tarun sajja
9th Dec 2017, 11:34 AM
Jonathan Álex
Jonathan Álex - avatar