Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Scanner;
- public class PalindromeTester
- {
- public static void main(String[] args)
- {
- String str, another = "y";
- int left, right;
- Scanner scan = new Scanner(System.in);
- while(another.equalsIgnoreCase("y"))
- {
- System.out.println("Enter a potential palindrome:");
- str = scan.nextLine();
- for(int i = 0; i < str.length(); i++)
- {
- if(Character.isLetterOrDigit(str.charAt(i)) == false)
- {
- str = str.replace(str.substring(i, i+1), "");
- i--;
- }
- System.out.println(str.substring(i,i+1) + i);
- System.out.println(str.length());
- }
- str = str.toLowerCase();
- System.out.println(str);
- if (str.length() > 0)
- {
- left = 0;
- right = str.length() - 1;
- while(str.charAt(left) == str.charAt(right) && left < right)
- {
- left++;
- right--;
- }
- System.out.println();
- if(left < right)
- System.out.println("That string is NOT a palindrome.");
- else
- System.out.println("That string IS a palindrome.");
- }
- else
- {
- System.out.println("The string you entered wasn't valid.");
- }
- System.out.println();
- System.out.print("Test another palindrome (y/n)? ");
- another = scan.nextLine();
- }
- }
- }
Add Comment
Please, Sign In to add comment