Results 1 to 10 of 10
  1. #1
    codec is offline Member
    Join Date
    Feb 2013
    Posts
    8
    Rep Power
    0

    Default Palindrome reversal help please :)

    Hello guys,

    I'm working with palindromes. My code below works fine with checking input for palindromes. But now I need help with code for my public static String(s1) method at very bottom. I need to use a stack to reverse the original input from the user and create a new reversed string with StringBuffer. I don't know how to implement this. Please kindly help, thank you.

    public class Palindrome {


    public static void main(String[ ] args)
    {
    Scanner console = new Scanner(System.in);
    String line;
    do
    {
    System.out.print("Your expression (or press enter to end): ");
    line = console.nextLine( );
    line = line.toUpperCase();

    if (is_palindrome(line))
    System.out.println("That is a palindrome.");
    else
    System.out.println("That is not a palindrome.");
    }
    while (line.length( ) != 0);
    }


    public static boolean is_palindrome(String input)
    {
    Queue<Character> q = new LinkedList<Character>( );
    Stack<Character> s = new Stack<Character>( );
    Character letter;
    int mismatches = 0;
    int i;

    for (i = 0; i < input.length( ); i++)
    {
    letter = input.charAt(i);
    if (Character.isLetter(letter))
    {
    q.add(letter);
    s.push(letter);
    }
    }

    while (!q.isEmpty( ))
    {
    if (q.remove( ) != s.pop( ))
    mismatches++;
    }

    return (mismatches == 0);
    }

    public static String(s1)
    {
    ****help here****
    }

    }

  2. #2
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    United States
    Posts
    2,949
    Rep Power
    4

    Default Re: Palindrome reversal help please :)

    First, please put your code between [code][/code] tags. So what have you tried thus far? To do what you want you need to iterate thru a string and build a new string in reverse order. First, try to iterate thru a string if you aren't sure how. Also check out the StringBuffer API and see if any routines trigger an idea.

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  3. #3
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    United States
    Posts
    2,949
    Rep Power
    4

    Default Re: Palindrome reversal help please :)

    A question. Are you required to use StringBuffer? StringBuilder is preferred since it is more efficient (no synchronization). According to at least one of the JDK architects, StringBuffer is obsolete.

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  4. #4
    codec is offline Member
    Join Date
    Feb 2013
    Posts
    8
    Rep Power
    0

    Default Re: Palindrome reversal help please :)

    Quote Originally Posted by jim829 View Post
    First, please put your code between [code][/code] tags. So what have you tried thus far? To do what you want you need to iterate thru a string and build a new string in reverse order. First, try to iterate thru a string if you aren't sure how. Also check out the StringBuffer API and see if any routines trigger an idea.
    Regards,
    Jim
    Would it be something like this?

    Java Code:
    public static String reverseString(String s1)
       {
    for (String part : s1.split(" ")) {
        System.out.print(new StringBuffer(part).reverse().toString());
        System.out.print(" ");
    }
            return null;

  5. #5
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    United States
    Posts
    2,949
    Rep Power
    4

    Default Re: Palindrome reversal help please :)

    That will work if you want to reverse each word which is separated by a space. One observation. If you are just going to return null, why not use void as the return type and then you can eliminate the "return null" statement.

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  6. #6
    codec is offline Member
    Join Date
    Feb 2013
    Posts
    8
    Rep Power
    0

    Default Re: Palindrome reversal help please :)

    I don't think that satisfies what I need to do. I have to use stack and either stringbuilder or string buffer.
    I think this is closer to what I need but it's not stringbuilder :/ I'm a noob. It says to use public static String reverseString(Strings1) as method.

    Java Code:
       public static String reverseString(String s1)
       {
                    Stack stack = new Stack();
                    for (int i = 0; i < s1.length(); i++) {
                            stack.push(s1.substring(i, i + 1));
                    }
                    String strrev = "";
                    while (!stack.isEmpty()) {
                            strrev += stack.pop();
                    }
                    return strrev;
    }
    }

  7. #7
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    United States
    Posts
    2,949
    Rep Power
    4

    Default Re: Palindrome reversal help please :)

    If it works, then that is the first step. However, using Stack is probably overkill and you said you were supposed to use StringBuilder or StringBuffer. I would check out the insert methods of those classes and see if that helps.

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  8. #8
    codec is offline Member
    Join Date
    Feb 2013
    Posts
    8
    Rep Power
    0

    Default Re: Palindrome reversal help please :)

    I can find stack reversals and stringbuilder reversals but not a combo of the two. :(

  9. #9
    codec is offline Member
    Join Date
    Feb 2013
    Posts
    8
    Rep Power
    0

    Default Re: Palindrome reversal help please :)

    How do I get the method reverseString(s1) to be called to my top class?

    Java Code:
    public class JavaApplication16 {
    {
    	Scanner console = new Scanner(System.in);      
    	String line;    
          do
          {
    	 System.out.print("Your expression (or press enter to end): ");
             line = console.nextLine( );
             line = line.toUpperCase();
             
    	 if (is_palindrome(line))
    	    System.out.println("That is a palindrome.");
    	 else
    	    System.out.println("That is not a palindrome.");
          }
          while (line.length( ) != 0);
       }
        System.out.println (reverseString.strrev);
    
    
       public static boolean is_palindrome(String input) 
       {   
          Queue<Character> q = new LinkedList<Character>( );
          Stack<Character> s = new Stack<Character>( );
          Character letter;   
          int mismatches = 0; 
          int i;              
          
          for (i = 0; i < input.length( ); i++)
          {
    	 letter = input.charAt(i);
             if (Character.isLetter(letter))
             {
                q.add(letter);
                s.push(letter);
             }
          }
          
          while (!q.isEmpty( ))
          {
             if (q.remove( ) != s.pop( ))
                mismatches++;
          }
    
          return (mismatches == 0); 
       }
       
       public static String reverseString(String s1)
       {
                    Stack stack = new Stack();
                    for (int i = 0; i < s1.length(); i++) {
                            stack.push(s1.substring(i, i + 1));
                    }
                    String strrev = "";
                    while (!stack.isEmpty()) {
                            strrev += stack.pop();
                    }
                    return strrev;
    
    }
    
    }

  10. #10
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    United States
    Posts
    2,949
    Rep Power
    4

    Default Re: Palindrome reversal help please :)

    Since it is a static method, prepend it with the name of its enclosing class and a period.

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

Similar Threads

  1. palindrome
    By rochellelising in forum New To Java
    Replies: 1
    Last Post: 08-24-2011, 01:03 PM
  2. Palindrome Recursion
    By jk336699 in forum New To Java
    Replies: 6
    Last Post: 08-24-2011, 06:02 AM
  3. Palindrome
    By leepikamukharji in forum New To Java
    Replies: 2
    Last Post: 04-29-2011, 03:20 PM
  4. Palindrome
    By pinkdreammsss in forum Java Applets
    Replies: 8
    Last Post: 05-04-2010, 03:59 PM
  5. HELP...Palindrome
    By d7o0om in forum New To Java
    Replies: 12
    Last Post: 11-13-2009, 03:32 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
  •