Advertisement
Guest User

Untitled

a guest
Oct 22nd, 2019
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.63 KB | None | 0 0
  1. import java.util.Scanner;
  2. public class Palindromes
  3. {
  4. /**
  5. * This program lets the user input some text and
  6. * prints out whether or not that text is a palindrome.
  7. */
  8. public static void main(String[] args)
  9. {
  10. // Create user input and let user know whether their word is a palindrome or not!
  11. Scanner input = new Scanner(System.in);
  12. System.out.println("Type in your text: ");
  13. String str = input.nextLine();
  14.  
  15.  
  16. if(isPalindrome(str))
  17. {
  18. System.out.println("Your word is a palindrome!");
  19. }
  20. else
  21. {
  22. System.out.println("Not a palindrome :(");
  23. }
  24. }
  25.  
  26. /**
  27. * This method determines if a String is a palindrome,
  28. * which means it is the same forwards and backwards.
  29. *
  30. * @param text The text we want to determine if it is a palindrome.
  31. * @return A boolean of whether or not it was a palindrome.
  32. */
  33. public static boolean isPalindrome(String text)
  34. {
  35. // Your code goes here!
  36. boolean isPal = false;
  37. if(reverse(text).equals(text))
  38. {
  39. isPal = true;
  40. }
  41.  
  42. return isPal;
  43. }
  44.  
  45. /**
  46. * This method reverses a String.
  47. *
  48. * @param text The string to reverse.
  49. * @return The new reversed String.
  50. */
  51. public static String reverse(String text)
  52. {
  53. // Your code goes here!
  54. String reverse = "";
  55. for(int i = text.length() - 1; i >= 0; i--)
  56. {
  57. reverse = reverse + text.charAt(i);
  58. }
  59. return reverse;
  60. }
  61.  
  62. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement