View Single Post
  #1 (permalink)  
Old 11-04-2007, 10:59 PM
toad toad is offline
Member
 
Join Date: Oct 2007
Posts: 11
toad is on a distinguished road
reversing a string
I have written this program to check a palindrome.

Code:
import javax.swing.JOptionPane; public class Exercise8_2 { public static void main(String[] args) { // Prompt the user to enter a string String s = JOptionPane.showInputDialog( null, "Enter a string:", "Exercise8_2 Input", JOptionPane.QUESTION_MESSAGE); if (isPalindrome(s)) { System.out.println(s + " is a palindrome"); } else { System.out.println(s + " is not a palindrome"); } } /** Check if a string is a palindrome */ public static boolean isPalindrome(String s) { // The index of the first character in the string int low = 0; // The index of the last character in the string int high = s.length() - 1; while (low < high) { if (s.charAt(low) != s.charAt(high)) return false; // Not a palindrome low++; high--; } return true; // The string is a palindrome } }
I now need to rewrite this using a new string that is a reversal of the string and compare the two to determine whether the string is a palindrome. My reverse method needs to use the following header:

public static String reverse(String s)


Thanks for your help. The first part came pretty easy but the reversal is throwing me for a loop.

Last edited by JavaBean : 11-04-2007 at 11:23 PM. Reason: [code] tag is fixed.
Reply With Quote
Sponsored Links