Guest User

Untitled

a guest
Dec 11th, 2017
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.27 KB | None | 0 0
  1. import java.util.Scanner;
  2.  
  3. public class PalindromeTester
  4. {
  5.  
  6.     public static void main(String[] args)
  7.     {
  8.         String str, another = "y";
  9.         int left, right;
  10.        
  11.         Scanner scan = new Scanner(System.in);
  12.        
  13.         while(another.equalsIgnoreCase("y"))
  14.         {
  15.             System.out.println("Enter a potential palindrome:");
  16.             str = scan.nextLine();
  17.             for(int i = 0; i < str.length(); i++)
  18.             {
  19.                 if(Character.isLetterOrDigit(str.charAt(i)) == false)
  20.                 {
  21.                     str = str.replace(str.substring(i, i+1), "");
  22.                     i--;
  23.                 }
  24.                 System.out.println(str.substring(i,i+1) + i);
  25.                 System.out.println(str.length());
  26.             }
  27.             str = str.toLowerCase();
  28.             System.out.println(str);
  29.            
  30.            
  31.            
  32.             if (str.length() > 0)
  33.             {
  34.                
  35.                 left = 0;
  36.                 right = str.length() - 1;
  37.                
  38.                 while(str.charAt(left) == str.charAt(right) && left < right)
  39.                 {
  40.                     left++;
  41.                     right--;
  42.                 }
  43.                
  44.                 System.out.println();
  45.                
  46.                 if(left < right)
  47.                     System.out.println("That string is NOT a palindrome.");
  48.                 else
  49.                     System.out.println("That string IS a palindrome.");
  50.             }
  51.             else
  52.             {
  53.                 System.out.println("The string you entered wasn't valid.");
  54.             }
  55.                
  56.             System.out.println();
  57.             System.out.print("Test another palindrome (y/n)? ");
  58.             another = scan.nextLine();
  59.         }
  60.     }
  61. }
Add Comment
Please, Sign In to add comment