Skip to content

Instantly share code, notes, and snippets.

@fado
Forked from LordNairu/PalindromeChecker.java
Last active August 29, 2015 13:56
Show Gist options
  • Save fado/8904213 to your computer and use it in GitHub Desktop.
Save fado/8904213 to your computer and use it in GitHub Desktop.
package practicalonep2.uk.ac.qub;
import java.util.Scanner;
public class Palindrome {
public static void main(String args[]){
String original, reverse="";
Scanner scanner = new Scanner(System.in);
System.out.println("Enter a word or sentence to check if it is a palindrome:");
original = scanner.nextLine();
scanner.close();
int length = original.length();
for ( int counter = length -1 ; counter >= 0 ; counter-- ){
reverse += original.charAt(counter);
}
if (original.replaceAll("\\s+","").equalsIgnoreCase(reverse.replaceAll("\\s+",""))){
System.out.printf("'%s' is a palindrome.",original);
}else{
System.out.printf("'%s' is not a palindrome.",original);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment