Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Linux Archive
Java Tips
Java Tips Blog

Sponsored Links





Welcome to the Java Forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:

  • have access to post topics
  • communicate privately with other members (PM)
  • not see advertisements between posts
  • have the possibility to earn one of our surprises if you are an active member
  • access many other special features that will be introduced later.

Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact us.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 11-04-2007, 10:59 PM
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.
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 11-04-2007, 11:25 PM
JavaBean's Avatar
Moderator
 
Join Date: May 2007
Posts: 1,272
JavaBean is on a distinguished road
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.
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 11-05-2007, 12:38 PM
Member
 
Join Date: Jun 2007
Location: Colombo, Sri Lanka
Posts: 31
hiranya is on a distinguished road
Hi,

Check this out

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; }
Regards,
Hiranya
__________________

To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
Bookmark Post in Technorati
Reply With Quote
  #4 (permalink)  
Old 11-06-2007, 09:59 PM
Member
 
Join Date: Nov 2007
Posts: 7
assamhammad is on a distinguished road
may God bless u all
Bookmark Post in Technorati
Reply With Quote
  #5 (permalink)  
Old 11-06-2007, 10:55 PM
JavaBean's Avatar
Moderator
 
Join Date: May 2007
Posts: 1,272
JavaBean is on a distinguished road
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..
Bookmark Post in Technorati
Reply With Quote
  #6 (permalink)  
Old 11-07-2007, 11:13 AM
Member
 
Join Date: Nov 2007
Posts: 7
bar311 is on a distinguished road
Hi use StringBuffer, it's much easier
check this one....

Code:
public static String reverse(String s) { return (new StringBuffer(s)).reverse().toString(); }
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Reversing String mew New To Java 4 12-02-2007 11:42 PM
Using java.util.Scanner to search for a String in a String Java Tip Java Tips 0 11-20-2007 06:59 PM
reversing Strings Java Tip Java Tips 0 11-11-2007 10:24 PM
Help with insertName(String name) and deleteName(String name) trill New To Java 1 08-07-2007 09:29 AM
I can't seem to pass the value of a string variable into a string array mathias Java Applets 1 08-03-2007 12:52 PM


All times are GMT +3. The time now is 10:55 PM.


VBulletin, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org