/* Lesley Hogan * July 28th 2003 * StringMethodsTwo.java * More fun stuff with strings * Homework from: * http://teachertech.rice.edu/Participants/marilynt/Lessons/Exeter/Programming/MoreStrings.html */ package net.bunnie.dos; import net.bunnie.*; public class StringMethodsTwo { public static void main (String[] args) { String palindrome = ""; String palindromee = ""; String inputPal; String lastFirstName; String lastName; String firstName; String firstLastName = ""; String isPal = "Is not a palindrome."; char[] punctuation = {'.', ',', '\'', '!', ' '}; char comma = ','; int commaAt = 0; boolean palBool = true; boolean palBoole = true; KeyboardReader r = new KeyboardReader(); Echo.e("Please input a palindrome: "); inputPal = r.readLine().toLowerCase(); Echo.e("Please input name Last, First: "); lastFirstName = r.readLine(); Echo.e("\n"); // test for palindrome // first for loop: forwards string without punct // second for loop: backwards string without punct // if equal, palindrome for (int i = 0; i <= inputPal.length() - 1; i++) { palBoole = true; for (int z = 0; z <= punctuation.length - 1; z++) { if (inputPal.charAt(i) == punctuation[z]) { palBoole = false; } } if (palBoole != false) { palindromee += inputPal.charAt(i); } } for (int i = inputPal.length() - 1; i >= 0; i--) { palBool = true; for (int z = 0; z <= punctuation.length - 1; z++) { if (inputPal.charAt(i) == punctuation[z]) { palBool = false; } } if (palBool != false) { palindrome += inputPal.charAt(i); } } if (palindrome.equals(palindromee)) { isPal = "Is a palindrome."; } // first name last name, get comma position and substrings for (int i = 0; i <= lastFirstName.length() - 1; i++) { if (lastFirstName.charAt(i) == comma) { commaAt = i; } } lastName = lastFirstName.substring(0, commaAt); firstName = lastFirstName.substring(commaAt + 2, lastFirstName.length()); firstLastName = firstName + " " + lastName; Echo.e("Your palindrome: " + isPal + "\n"); Echo.e("First Last: " + firstLastName + "\n\n"); } }