Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > String Reverse

Reply
Thread Tools

String Reverse

 
 
Amit Jain
Guest
Posts: n/a
 
      10-08-2007
How can I reverse a given string without using String or StringBuffer
class method.

 
Reply With Quote
 
 
 
 
Lasse Reichstein Nielsen
Guest
Posts: n/a
 
      10-08-2007
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.'
 
Reply With Quote
 
 
 
 
Mike Schilling
Guest
Posts: n/a
 
      10-09-2007

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


 
Reply With Quote
 
Roedy Green
Guest
Posts: n/a
 
      10-09-2007
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
 
Reply With Quote
 
Daniel Pitts
Guest
Posts: n/a
 
      10-10-2007
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/>
 
Reply With Quote
 
Piotr Kobzda
Guest
Posts: n/a
 
      10-10-2007
Amit Jain wrote:
> How can I reverse a given string without using String or StringBuffer
> class method.


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


piotr
 
Reply With Quote
 
Piotr Kobzda
Guest
Posts: n/a
 
      10-10-2007
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
 
Reply With Quote
 
Piotr Kobzda
Guest
Posts: n/a
 
      10-10-2007
Piotr Kobzda wrote:
> ... Thus, one should even try to do
> that!


NO-ONE of course!


piotr
 
Reply With Quote
 
Roedy Green
Guest
Posts: n/a
 
      10-10-2007
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
 
Reply With Quote
 
Walter Milner
Guest
Posts: n/a
 
      10-10-2007
On 10 Oct, 12:37, Roedy Green <see_webs...@mindprod.com.invalid>
wrote:
> On Wed, 10 Oct 2007 11:52:11 +0200, Piotr Kobzda <pi...@gazeta.pl>
> 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


 
Reply With Quote
 
 
 
Reply

Thread Tools

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

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


Similar Threads
Thread Thread Starter Forum Replies Last Post
reverse string, how to print string and not decimals? ssecorp C Programming 47 08-08-2008 06:48 PM
Accidental Reverse order String display Roedy Green Java 9 08-11-2005 01:30 PM
String Reverse Question Rakesh C Programming 45 04-20-2004 02:06 AM
Re: Reverse function of Server.MapPath(string file)? Curt_C [MVP] ASP .Net 0 01-22-2004 05:36 PM
Stacks Queues Reverse Reverse Polish dogbite C++ 4 10-10-2003 05:06 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57