Advertisement
Guest User

Untitled

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