Advertisement
Guest User

Untitled

a guest
Nov 23rd, 2014
150
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.69 KB | None | 0 0
  1. import java.util.Scanner;
  2.  
  3. public class TextLab04v100Wu
  4. {
  5. public static void main (String args[])
  6. {
  7. System.out.println("\nTextLab04v100Wu\n");
  8. Scanner input = new Scanner(System.in);
  9. boolean notFinished = false;
  10. do
  11. {
  12. System.out.print("Enter a string ===>> ");
  13. String str = input.nextLine();
  14. System.out.println();
  15. System.out.println("Entered String: " + str);
  16. System.out.println("Palindrome: " + Palindrome.isPal(str));
  17. System.out.println("Almost Palindrome: " + Palindrome.almostPal(str)); // used only for the 100 point version
  18. System.out.print("Do you wish to repeat this program [Y/N]? ===>> ");
  19. String repeat = input.nextLine();
  20. notFinished = (repeat.equals("Y")) || (repeat.equals("y"));
  21. System.out.println();
  22. }
  23. while (notFinished);
  24. }
  25. }
  26.  
  27.  
  28.  
  29. class Palindrome
  30. {
  31. /*
  32. * Precondition: s is an arbitrary String.
  33. * Postcondition: The value of true is returned if s is a Palindrome, false otherwise.
  34. * Note: >>>>> This method is required for both the 80 point and the 100 point versions <<<<<
  35. */
  36. public static boolean isPal(String s)
  37. {
  38. s = s.toUpperCase();
  39. for (int i=0; i<s.length()/2; i++)
  40. {
  41. if (!(s.substring(i, i+1).equals(s.substring(s.length()-1-i, s.length()-i))))
  42. {
  43. return false;
  44. }
  45. }
  46. return true;
  47. }
  48.  
  49. /*
  50. * Precondition: s is a String of one character.
  51. * Postcondition: The value of true is returned if s is a letter and false otherwise.
  52. * Note: >>>>> This method is only completed for the 100 point version <<<<<
  53. */
  54. public static boolean isLetter(String letter)
  55. {
  56. return ! (letter.toUpperCase().equals(letter.toLowerCase()));
  57. }
  58.  
  59. /*
  60. * Precondition: s is an arbitrary String.
  61. * Postcondition: All non-letter characters are removed from s, and this "purged" String is returned.
  62. * Note: >>>>> This method is only completed for the 100 point version <<<<<
  63. */
  64. public static String purge(String s)
  65. {
  66. String newString = "";
  67.  
  68. for (int i = 0; i < s.length(); i++)
  69. {
  70. String sub = s.substring(i, i+1);
  71.  
  72. if (!sub.equals(",") && !sub.equals(" ") && !sub.equals("."))
  73. {
  74. newString += sub; //substituted into original string
  75. }
  76. }
  77.  
  78. return newString;
  79. }
  80.  
  81. /*
  82. * Precondition: s is an arbitrary String.
  83. * Postcondition: After purging all non-letter characters from s,
  84. * the value of true is returned if the resulting String is a Palindrome, false otherwise.
  85. * Note: >>>>> This method is only completed for the 100 point version <<<<<
  86. */
  87. public static boolean almostPal(String s)
  88. {
  89. return isPal(purge(s));
  90. }
  91. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement