PalindromeTester - } } return converted; }

Info iconThis preview shows pages 1–2. Sign up to view the full content.

View Full Document Right Arrow Icon
//******************************************************************** // PalindromeTester.java Author: Lewis/Loftus // // Solution to Programming Project 5.9 //******************************************************************** / import javax.swing.JOptionPane; i public class PalindromeTester { //----------------------------------------------------------------- // Removes whitespace and punction from a string. Converts all // characters to lowercase. //----------------------------------------------------------------- private static String convertString(String s) { String converted = ""; char current; for (int i=0; i<s.length(); i++) { current = s.charAt(i); if (Character.isLetterOrDigit(current)) // only count letters and digits { if (Character.isUpperCase(current)) // convert to lowercase if needed current = Character.toLowerCase(current); converted += current;
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Background image of page 2
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: } } return converted; } //-----------------------------------------------------------------// Tests strings to see if they are palindromes. //-----------------------------------------------------------------public static void main (String args) { String str, result, in; int left, right; int another; do { in = JOptionPane.showInputDialog (&quot;Enter a potential palindrome:&quot;); str = convertString(in); left = 0; right = str.length() - 1; while (str.charAt(left) == str.charAt(right) &amp;&amp; left &lt; right) { left++; right--; } result = &quot;\&quot;&quot; + in + &quot;\&quot;&quot;; if (left &lt; right) result += &quot; is NOT a palindrome.&quot;; else result += &quot; IS a palindrome.&quot;; JOptionPane.showMessageDialog(null, result); another = JOptionPane.showConfirmDialog(null,&quot;Test another palindrome? &quot;); } while (another == JOptionPane.YES_OPTION); } }...
View Full Document

Page1 / 2

PalindromeTester - } } return converted; }

This preview shows document pages 1 - 2. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online