Results 1 to 6 of 6
  1. #1
    toad is offline Member
    Join Date
    Oct 2007
    Posts
    11
    Rep Power
    0

    Default reversing a string

    I have written this program to check a palindrome.

    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
    }
    }
    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 09:23 PM. Reason: [code] tag is fixed.

  2. #2
    JavaBean's Avatar
    JavaBean is offline Moderator
    Join Date
    May 2007
    Posts
    1,270
    Rep Power
    9

    Default

    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.

  3. #3
    hiranya is offline Member
    Join Date
    Jun 2007
    Location
    Colombo, Sri Lanka
    Posts
    32
    Rep Power
    0

    Default

    Hi,

    Check this out

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

  4. #4
    assamhammad is offline Member
    Join Date
    Nov 2007
    Posts
    7
    Rep Power
    0

    Default

    may God bless u all

  5. #5
    JavaBean's Avatar
    JavaBean is offline Moderator
    Join Date
    May 2007
    Posts
    1,270
    Rep Power
    9

    Default

    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..

  6. #6
    bar311 is offline Member
    Join Date
    Nov 2007
    Posts
    7
    Rep Power
    0

    Default Hi use StringBuffer, it's much easier

    check this one....

    Java Code:
    public static String reverse(String s) {
       return (new StringBuffer(s)).reverse().toString();
    }

Similar Threads

  1. Reversing String
    By mew in forum New To Java
    Replies: 4
    Last Post: 12-02-2007, 09:42 PM
  2. Replies: 0
    Last Post: 11-20-2007, 04:59 PM
  3. reversing Strings
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 11-11-2007, 08:24 PM
  4. Replies: 1
    Last Post: 08-07-2007, 07:29 AM
  5. Replies: 1
    Last Post: 08-03-2007, 10:52 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •