Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.*;
- public class palindrome {
- public static void main(String[] args) {
- Scanner input = new Scanner(System.in);
- System.out.println("Please enter the string you want evaluated for palindromeness: ");
- String palindromeCandidate = input.nextLine().toLowerCase(); //changes all to lowercase
- palindromeCandidate=palindromeCandidate.replaceAll(" ",""); //removes spaces
- for (int i=0; i<palindromeCandidate.length();i++){ //removes punctuation...hopefully
- if(Character.isLetter(palindromeCandidate.charAt(i))==false)
- palindromeCandidate=palindromeCandidate.replace(palindromeCandidate.substring(i,i+1), "");
- }
- if (isPalinedrome(palindromeCandidate)) {
- System.out.println(palindromeCandidate + " is a palindrome");
- } else {
- System.out.println(palindromeCandidate + " is not a palindrome");
- }
- //input.close();
- }
- public static boolean isPalinedrome(String str) {
- for (int i = 0; i < str.length(); i++) {
- char leftHand = str.charAt(i);
- char rightHand = str.charAt(str.length() - 1 - i);
- if (leftHand != rightHand){
- return false;
- }
- }
- return true;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement