Results 1 to 6 of 6
Thread: reversing a string
- 11-04-2007, 08:59 PM #1
Member
- Join Date
- Oct 2007
- Posts
- 11
- Rep Power
- 0
reversing a string
I have written this program to check 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:Java 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 } }
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 09:23 PM. Reason: [code] tag is fixed.
- 11-04-2007, 09:25 PM #2
Create a new String object and append each character of the second String to the new one. A for loop starting from the last character of the first string going towards its beginning is enough. Check substring() method of String class to get one character at each step of your loop.
- 11-05-2007, 10:38 AM #3
Member
- Join Date
- Jun 2007
- Location
- Colombo, Sri Lanka
- Posts
- 32
- Rep Power
- 0
Hi,
Check this out
Regards,Java Code:public String reverse(String s) { char[] array=s.toCharArray(); String result=""; for (int i=array.length-1; i>=0; i--) { result+=array[i]; } return result; }
Hiranya
- 11-06-2007, 07:59 PM #4
Member
- Join Date
- Nov 2007
- Posts
- 7
- Rep Power
- 0
may God bless u all
- 11-06-2007, 08:55 PM #5
assamhammad, please stop posting this kind of (out of topic) messages. you already have 4 of this type of message out of your 7 posts in the last hour. This is called spam..
- 11-07-2007, 09:13 AM #6
Member
- Join Date
- Nov 2007
- Posts
- 7
- Rep Power
- 0
Similar Threads
-
Reversing String
By mew in forum New To JavaReplies: 4Last Post: 12-02-2007, 09:42 PM -
Using java.util.Scanner to search for a String in a String
By Java Tip in forum Java TipReplies: 0Last Post: 11-20-2007, 04:59 PM -
reversing Strings
By Java Tip in forum Java TipReplies: 0Last Post: 11-11-2007, 08:24 PM -
Help with insertName(String name) and deleteName(String name)
By trill in forum New To JavaReplies: 1Last Post: 08-07-2007, 07:29 AM -
I can't seem to pass the value of a string variable into a string array
By mathias in forum Java AppletsReplies: 1Last Post: 08-03-2007, 10:52 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks