String Reverse

Discussion in 'Java' started by Amit Jain, Oct 8, 2007.

  1. Amit Jain

    Amit Jain Guest

    How can I reverse a given string without using String or StringBuffer
    class method.
     
    Amit Jain, Oct 8, 2007
    #1
    1. Advertising

  2. Amit Jain <> writes:

    > How can I reverse a given string without using String or StringBuffer
    > class method.


    1. Why would you?
    2. What String will you reverse if you don't use String?
    And how will you represent the result without using a String?
    3. Are you thinking of extracting the chars into an array and
    reversing them there? Take care with multi-char Unicode characters :)

    /L
    --
    Lasse Reichstein Nielsen -
    DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
    'Faith without judgement merely degrades the spirit divine.'
     
    Lasse Reichstein Nielsen, Oct 8, 2007
    #2
    1. Advertising

  3. "Lasse Reichstein Nielsen" <> wrote in message
    news:...
    > Amit Jain <> writes:
    >
    >> How can I reverse a given string without using String or StringBuffer
    >> class method.

    >
    > 1. Why would you?


    Because it's on his homework, of course.
     
    Mike Schilling, Oct 9, 2007
    #3
  4. Amit Jain

    Roedy Green Guest

    On Mon, 08 Oct 2007 12:19:36 -0700, Amit Jain <>
    wrote, quoted or indirectly quoted someone who said :

    >How can I reverse a given string without using String or StringBuffer
    >class method.


    This obviously an artificial exercise since no one in their right mind
    would avoid the built-in StringBuffer/StringBuilder/String methods.

    The basic idea is a create char array the correct length, the fill it
    one by one from your original string in a loop. Then when you are
    done you convert the char[] to a String.

    See http://mindprod.com/jgloss/conversion.html
    --
    Roedy Green Canadian Mind Products
    The Java Glossary
    http://mindprod.com
     
    Roedy Green, Oct 9, 2007
    #4
  5. Amit Jain

    Daniel Pitts Guest

    Amit Jain wrote:
    > How can I reverse a given string without using String or StringBuffer
    > class method.
    >

    What is the sound of one hand clapping?
    You can't reverse a string unless you have a string, now can you?

    If you already have a string, you can *print* the reverse if it by
    counting backward from the end of it, and printing each character along
    the way.

    --
    Daniel Pitts' Tech Blog: <http://virtualinfinity.net/wordpress/>
     
    Daniel Pitts, Oct 10, 2007
    #5
  6. Amit Jain

    Piotr Kobzda Guest

    Amit Jain wrote:
    > How can I reverse a given string without using String or StringBuffer
    > class method.


    new StringBuilder( givenString ).reverse().toString();


    piotr
     
    Piotr Kobzda, Oct 10, 2007
    #6
  7. Amit Jain

    Piotr Kobzda Guest

    Daniel Pitts wrote:
    > Amit Jain wrote:
    >> How can I reverse a given string without using String or StringBuffer
    >> class method.
    >>

    > What is the sound of one hand clapping?


    Whoosh? :)

    > If you already have a string, you can *print* the reverse if it by
    > counting backward from the end of it, and printing each character along
    > the way.


    That way some methods of String will still be used indirectly (when
    allowed, I prefer my earlier suggestion).

    The only way I know to achieve the result without touching any of String
    methods is to reflectively access String's internals (i.e. value,
    offset, and count fields). And then create a new String (we can use
    constructors, they are not a methods) with the reversed chars of the
    value array copy. That way is of course never guarantied to work, and
    is rather awful hack than the solution. Thus, one should even try to do
    that!


    piotr
     
    Piotr Kobzda, Oct 10, 2007
    #7
  8. Amit Jain

    Piotr Kobzda Guest

    Piotr Kobzda wrote:
    > ... Thus, one should even try to do
    > that!


    NO-ONE of course! :)


    piotr
     
    Piotr Kobzda, Oct 10, 2007
    #8
  9. Amit Jain

    Roedy Green Guest

    On Wed, 10 Oct 2007 11:52:11 +0200, Piotr Kobzda <>
    wrote, quoted or indirectly quoted someone who said :

    >new StringBuilder( givenString ).reverse().toString();


    I think the intent of the exercise is to solve it without using a
    built-in reverse method. The prof wants his students to do a simple
    char by char string building exercise.

    --
    Roedy Green Canadian Mind Products
    The Java Glossary
    http://mindprod.com
     
    Roedy Green, Oct 10, 2007
    #9
  10. On 10 Oct, 12:37, Roedy Green <>
    wrote:
    > On Wed, 10 Oct 2007 11:52:11 +0200, Piotr Kobzda <>
    > wrote, quoted or indirectly quoted someone who said :
    >
    > >new StringBuilder( givenString ).reverse().toString();

    >
    > I think the intent of the exercise is to solve it without using a
    > built-in reverse method. The prof wants his students to do a simple
    > char by char string building exercise.
    >
    > --
    > Roedy Green Canadian Mind Products
    > The Java Glossaryhttp://mindprod.com


    Hey Amit are you still with us?

    The standard recursive and fun way to do this is (in pseudo-code)

    if length of string is 1, return string
    else
    return reverse(string without first character) joined with first
    character
     
    Walter Milner, Oct 10, 2007
    #10
  11. Amit Jain

    Chris Dollin Guest

    Walter Milner wrote:

    > The standard recursive and fun way to do this is (in pseudo-code)
    >
    > if length of string is 1, return string
    > else
    > return reverse(string without first character) joined with first
    > character


    BOOM.

    (reverse the empty string.)

    --
    Chris "Ivanova, one day on" Dollin

    Hewlett-Packard Limited registered no:
    registered office: Cain Road, Bracknell, Berks RG12 1HN 690597 England
     
    Chris Dollin, Oct 10, 2007
    #11
  12. Amit Jain

    Stefan Ram Guest

    Amit Jain <> writes:
    >How can I reverse a given string without
    >using String or StringBuffer class method.


    The statement

    java.lang.System.out.println( "Hello!" );

    can be split in a model and a view.

    The model (»"Hello!«) is the internal storage of the code
    points H-e-l-l-o-!, the View (»println«) displays this as

    Hello!

    One can as well adopt the point of view that for the string
    model it makes no sense to »reverse« anything, because the
    same model can also be viewed as a storage of the reversed
    string, because whether to display the stored code points
    H-e-l-l-o-! as

    Hello!

    or as

    !olleH

    is solely a question of the View, just as the color of the
    characters used to display it or the size of the font.

    From such a point of view, every string model already /is/
    also a model of the reversed string, without the need for
    an explicit computation to »determine« the reverse string.

    (Admittedly, this point of view is not helpful in some cases.)
     
    Stefan Ram, Oct 10, 2007
    #12
  13. Amit Jain

    Piotr Kobzda Guest

    Roedy Green wrote:

    > I think the intent of the exercise is to solve it without using a
    > built-in reverse method. The prof wants his students to do a simple
    > char by char string building exercise.


    Yes, probably.

    But I was wondering if it's possible to correctly answer the OP's
    question taken "as is", without making any unsound assumptions.

    The problematic is even access to the String's length or chars without
    using its methods.

    Fortunately there is no mention about StringBuilder in the OP's
    question, that's why I used it.


    BTW -- If the prof intent was different, he, or she should clearly state
    that. Otherwise, there is a huge number of solutions other than just
    replace char by char.

    Just one example more:

    String reversed = "";
    for(String s : java.util.regex.
    Pattern.compile("").split( givenString )) {
    reversed = s + reversed;
    }


    piotr
     
    Piotr Kobzda, Oct 10, 2007
    #13
  14. Amit Jain

    Guest

    On Oct 10, 7:37 am, Roedy Green <>
    wrote:
    > On Wed, 10 Oct 2007 11:52:11 +0200, Piotr Kobzda <>
    > wrote, quoted or indirectly quoted someone who said :
    >
    > >new StringBuilder( givenString ).reverse().toString();

    >
    > I think the intent of the exercise is to solve it without using a
    > built-in reverse method. The prof wants his students to do a simple
    > char by char string building exercise.
    >


    ....Then he should make them do it in C.
     
    , Oct 10, 2007
    #14
  15. "Piotr Kobzda" <> wrote in message
    news:fei7cb$638$...
    > Amit Jain wrote:
    >> How can I reverse a given string without using String or StringBuffer
    >> class method.

    >
    > new StringBuilder( givenString ).reverse().toString();


    The Javadoc says that this handles surrogate pairs correctly, making it an
    excellent answer.
     
    Mike Schilling, Oct 10, 2007
    #15
  16. Amit Jain

    Amit Jain Guest

    for example i have string

    String temp = "Java";

    and I need some sort of code which reverse this string without using
    any String or other class method
     
    Amit Jain, Oct 11, 2007
    #16
  17. Amit Jain wrote:
    > for example i have string
    >
    > String temp = "Java";
    >
    > and I need some sort of code which reverse this string without using
    > any String or other class method


    You just restated your task without giving any additional clarification.
    What does "without using any String or other class method" mean?

    Your tutor probably means
    StringBuffer.reverse() and StringBuilder.reverse()
    but we don't know that.

    Are you allowed to use
    String.charAt() and StringBuffer.append()
    or
    String.toCharArray() and the String(char[]) constructor
    or any combination of the above?
     
    Thomas Schodt, Oct 11, 2007
    #17
  18. Amit Jain

    Daniel Pitts Guest

    Amit Jain wrote:
    > for example i have string
    >
    > String temp = "Java";
    >
    > and I need some sort of code which reverse this string without using
    > any String or other class method
    >

    First: In Java, Strings are immutable. You cannot reverse a String.
    Second: We will not do your homework. Tell us what you have tried so
    far, and we'll gladly give you suggestion on why it doesn't work. This
    is a relatively straight forward task. We've already passed this
    homework assignment, now its your turn.

    --
    Daniel Pitts' Tech Blog: <http://virtualinfinity.net/wordpress/>
     
    Daniel Pitts, Oct 11, 2007
    #18
  19. Amit Jain

    Malinbeg Guest

    Amit Jain wrote:
    > How can I reverse a given string without using String or StringBuffer
    > class method.
    >




    String reverse (String in)
    throws IOException
    {
    Reader reader = new StringReader(in);

    Stack<Character> stack = new Stack();

    int ch;

    while ((ch = reader.read()) != -1)
    {
    stack.push((char)ch);
    }


    Writer writer = new StringWriter();

    while (!stack.isEmpty())
    {
    writer.write(stack.pop());
    }


    String reversed = writer.toString();

    return reversed;
    }
     
    Malinbeg, Oct 12, 2007
    #19
  20. Amit Jain

    Lew Guest

    Malinbeg wrote:
    > String reverse (String in)
    > throws IOException


    To the OP:
    This is an example, bear in mind. In Real Life trap and log exceptions at the
    point of occurrence, then perform a strategic recovery (possibly to exit the
    app?).

    > {
    > Reader reader = new StringReader(in);


    Note the good use of an interface variable type with a concrete implementation
    run-time type. This is a best practice.

    > Stack<Character> stack = new Stack();


    Oops. "new Stack<Character>()".

    > int ch;


    The scope of "ch" is too wide. Put it into a for loop.

    > while ((ch = reader.read()) != -1)
    > {
    > stack.push((char)ch);
    > }
    >
    >
    > Writer writer = new StringWriter();


    This one cannot go into a for loop because the value is needed after the loop.

    > while (!stack.isEmpty())
    > {
    > writer.write(stack.pop());
    > }
    >
    >
    > String reversed = writer.toString();
    >
    > return reversed;
    > }


    There are ways that follow this pattern without using java.io, also.

    --
    Lew
     
    Lew, Oct 12, 2007
    #20
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Curt_C [MVP]
    Replies:
    0
    Views:
    1,918
    Curt_C [MVP]
    Jan 22, 2004
  2. Roedy Green
    Replies:
    9
    Views:
    1,618
    Jeff Schwab
    Aug 11, 2005
  3. dogbite
    Replies:
    4
    Views:
    694
    osmium
    Oct 10, 2003
  4. Rakesh

    String Reverse Question

    Rakesh, Apr 16, 2004, in forum: C Programming
    Replies:
    45
    Views:
    1,334
    Christopher Benson-Manica
    Apr 20, 2004
  5. ssecorp
    Replies:
    47
    Views:
    1,063
    Default User
    Aug 8, 2008
Loading...

Share This Page