Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Scanner;
- public class TextLab04v100Wu
- {
- public static void main (String args[])
- {
- System.out.println("\nTextLab04v100Wu\n");
- Scanner input = new Scanner(System.in);
- boolean notFinished = false;
- do
- {
- System.out.print("Enter a string ===>> ");
- String str = input.nextLine();
- System.out.println();
- System.out.println("Entered String: " + str);
- System.out.println("Palindrome: " + Palindrome.isPal(str));
- System.out.println("Almost Palindrome: " + Palindrome.almostPal(str)); // used only for the 100 point version
- System.out.print("Do you wish to repeat this program [Y/N]? ===>> ");
- String repeat = input.nextLine();
- notFinished = (repeat.equals("Y")) || (repeat.equals("y"));
- System.out.println();
- }
- while (notFinished);
- }
- }
- class Palindrome
- {
- /*
- * Precondition: s is an arbitrary String.
- * Postcondition: The value of true is returned if s is a Palindrome, false otherwise.
- * Note: >>>>> This method is required for both the 80 point and the 100 point versions <<<<<
- */
- public static boolean isPal(String s)
- {
- s = s.toUpperCase();
- for (int i=0; i<s.length()/2; i++)
- {
- if (!(s.substring(i, i+1).equals(s.substring(s.length()-1-i, s.length()-i))))
- {
- return false;
- }
- }
- return true;
- }
- /*
- * Precondition: s is a String of one character.
- * Postcondition: The value of true is returned if s is a letter and false otherwise.
- * Note: >>>>> This method is only completed for the 100 point version <<<<<
- */
- public static boolean isLetter(String letter)
- {
- return ! (letter.toUpperCase().equals(letter.toLowerCase()));
- }
- /*
- * Precondition: s is an arbitrary String.
- * Postcondition: All non-letter characters are removed from s, and this "purged" String is returned.
- * Note: >>>>> This method is only completed for the 100 point version <<<<<
- */
- public static String purge(String s)
- {
- String newString = "";
- for (int i = 0; i < s.length(); i++)
- {
- String sub = s.substring(i, i+1);
- if (!sub.equals(",") && !sub.equals(" ") && !sub.equals("."))
- {
- newString += sub; //substituted into original string
- }
- }
- return newString;
- }
- /*
- * Precondition: s is an arbitrary String.
- * Postcondition: After purging all non-letter characters from s,
- * the value of true is returned if the resulting String is a Palindrome, false otherwise.
- * Note: >>>>> This method is only completed for the 100 point version <<<<<
- */
- public static boolean almostPal(String s)
- {
- return isPal(purge(s));
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement