almost palindrome java code example
Example 1: easy palindrome program in java
import java.util.*;
class PalindromeExample2
{
public static void main(String args[])
{
String original, reverse = "";
Scanner in = new Scanner(System.in);
System.out.println("Enter a string/number to check if it is a palindrome");
original = in.nextLine();
int length = original.length();
for ( int i = length - 1; i >= 0; i-- )
reverse = reverse + original.charAt(i);
if (original.equals(reverse))
System.out.println("Entered string/number is a palindrome.");
else
System.out.println("Entered string/number isn't a palindrome.");
}
}
Example 2: is palindrome method in java
package test
public boolean isPalindromString(String text){
String reverse = reverse(text);
if(text.equals(reverse))
{
return true;
}
return false;
}
public String reverse(String input)
{
if(input == null || input.isEmpty())
{
return input;
}
return input.charAt(input.length()- 1) + reverse
(input.substring(0, input.length() - 1));
}
Example 3: palindrome in java
public class PlaindromeTest1 {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.println("Enter your words");
String word = input.nextLine();
String reversedWord = "";
for(int i = word.length()-1; i>=0; i-- ){
reversedWord += word.charAt(i);
}
System.out.println(reversedWord);
boolean palindrome = word.equalsIgnoreCase(reversedWord);
System.out.println(palindrome);
String str = "Anna";
String result = "";
for(int i = str.length()-1; i>=0; i-- ){
result += str.substring(i, i+1);
}
boolean result1 = str.equalsIgnoreCase(result);
System.out.println(result1);
}