+ 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
4 Réponses
+ 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;
}
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.");
}
}
0
Here are my new tries, now using Python:
https://code.sololearn.com/ckzKZUhvplvr/?ref=app
https://code.sololearn.com/cdb28fBq2f90/?ref=app