Advertisement
Guest User

Untitled

a guest
Jun 19th, 2017
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.21 KB | None | 0 0
  1.  
  2. import java.util.Scanner;
  3.  
  4. /*
  5.  * Name: Will Costen
  6.  * PID: 7201-33275
  7.  * Test Plan
  8.  *  
  9.  */
  10. public class Palindrome {
  11.  
  12.     public static void main(String[] args) {
  13.          {
  14.              String s;
  15.                 Scanner keyboard = new Scanner(System.in);
  16.  
  17.                 System.out.println("Please enter a possible palindrome");
  18.                 s = keyboard.nextLine();
  19.            
  20.          
  21.              int n=s.length();
  22.  
  23.              String str="";
  24.              String s2="";
  25.              
  26.              for(int j=0;j<=s.length()-1;j++) //reads string forwards
  27.                  if (s.charAt(j)!=' '){
  28.                      s2+=s.charAt(j);
  29.                    
  30.                  }
  31.  
  32.              for(int i=n-1;i>=0;i--) //reads string backwards
  33.                  if (s.charAt(i)!=' ')
  34.                      str=str+s.charAt(i);
  35.                
  36.            
  37.                
  38.  
  39.              if(str.equalsIgnoreCase(s2)) //confirms palindrome
  40.  
  41.                      System.out.println(s+ " is a palindrome");
  42.  
  43.              else{
  44.                      System.out.println(str);
  45.                      System.out.println(s2);
  46.                      System.out.println(s+ " is not a palindrome");
  47.              }
  48.      }
  49.        
  50.        
  51.     // Java will read the entire line into the variable 'phrase'.
  52.  
  53.     }
  54.  
  55. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement