Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * @author Mines_of_Moria
- * Sunday, February 2nd, 2014
- */
- package palindrome;
- import java.util.Scanner;
- public class Palindrome {
- public static void main(String[] args) {
- String userString;
- Scanner kb = new Scanner(System.in);
- System.out.println("This program will attempt to tell you if your String is a palindrome" +
- " just as it is, a first class palindrome, or a second class palindrome.\n");
- System.out.printf("Please enter a String: ");
- userString = kb.nextLine();
- System.out.println("\nYou entered: " + userString + "\n");
- System.out.println("Method A uses String and Character methods to determine if your String" +
- " is a palindrome just as it is.\n");
- if (methodA(userString)) {
- System.out.println("Method A result: " + userString + " is a palindrome just as it is.\n");
- } else {
- System.out.println("Method A result: " + userString + " is NOT a palindrome just as it is.\n");
- }
- System.out.println("Method B uses the StringBuilder reverse method to tell us if a String" +
- " is a palindrome just as it is, without changing case or ignoring non-letter characters. \n" +
- "This is a first class palindrome.\n");
- if (methodB(userString)) {
- System.out.println("Method B result: " + userString + " is a first class palindrome.\n");
- } else {
- System.out.println("Method B result: " + userString + " is NOT a first class palindrome.\n");
- System.out.println("Method C tells us if the letters in a String form a palindrome if we " +
- "ignore case, white spaces, and punctuation. This is a second class palindrome.\n");
- if (methodC(userString)) {
- System.out.println("Method C result: " + userString + " is a second class palindrome.\n");
- } else {
- System.out.println("Method C result: " + userString + " is NOT a second class palindrome.\n");
- }
- }
- } // end main
- // The following checks the first and last characters are the same, then walks
- // further inside the string checking the results each time. It returns as soon
- // as the String isn't a palindrome.
- static boolean methodA(String userString) {
- int n = userString.length();
- for (int i=0;i<(n/2);i++) {
- if (userString.charAt(i) != userString.charAt(n - i - 1)) {
- return false;
- }
- }
- return true;
- }
- // The following returns if StringBuilder(user entered String) equals the
- // reverse of StringBuilder
- static boolean methodB(String userString) {
- String reversed = new StringBuilder(userString).reverse().toString();
- return userString.equals( reversed );
- }
- // The following writes a regular expression to match blanks and punctuation,
- // and provides and empty string ("") as the replacement. This returns
- // the original minus the characters that need ignored.
- static boolean methodC(String userString) {
- userString = userString.toLowerCase().replaceAll("\\W", "");
- return (methodB(userString));
- }
- /* This method reads data from the file into the array.
- * This array works with up to 100 elements.
- * Each line from the file will be one element in the array.
- * The parameter refers to the array in the main method.
- * The method returns the number of elements it uses.
- */
- } // end class Palindrome
Advertisement
Add Comment
Please, Sign In to add comment