
03-20-2010, 10:44 AM
|
|
Member
|
|
Join Date: Mar 2010
Posts: 5
Rep Power: 0
|
|
Reverse a string?
How can you reverse a string/number without using any loops or existing methods?
|
|

03-20-2010, 10:49 AM
|
|
Senior Member
|
|
Join Date: Sep 2008
Location: Voorschoten, the Netherlands
Posts: 3,322
Rep Power: 5
|
|
Originally Posted by cysquatch
|
|
How can you reverse a string/number without using any loops or existing methods?
|
For Strings have a look at the StringBuilder or StringBuffer classes. Numbers are quite a different cup of tea and a simple recursive method can do the job if the number is an integral number.
kind regards,
Jos
|
|

03-20-2010, 11:16 AM
|
|
Member
|
|
Join Date: Mar 2010
Posts: 5
Rep Power: 0
|
|
No sweat, thanks for that.
Also another required tip...can you explain how to read/put the methods and libraries into usable code at all? I know i'm a noob but seeing something such as this: BufferedReader (Java 2 Platform SE v1.4.2)
I'm a bit unsure of how to put it all into practice.
Thanks & Sorry
|
|

03-20-2010, 11:20 AM
|
|
Member
|
|
Join Date: Mar 2010
Posts: 5
Rep Power: 0
|
|
|
I have an idea for how I want to solve my problem:
Say for example someone enters a 3 digit number, 123
using the charAt(); function, i'd want to read the above 123 string into a new string in reverse. Is it possible to just say something like:
charAt(2);
append to result;
charAt(1);
append to result;
charAt(0);
append to result;
I'm just a bit new to this coding area.
|
|

03-20-2010, 11:27 AM
|
|
Senior Member
|
|
Join Date: Sep 2008
Location: Voorschoten, the Netherlands
Posts: 3,322
Rep Power: 5
|
|
Originally Posted by cysquatch
|
|
I have an idea for how I want to solve my problem
|
Why don't you give it a try:
|
Code:
|
public class MyClass {
public static void main(String[] args) {
StringBuilder sb= new StringBuilder();
// your code goes here
}
} |
compile and run it:
|
Code:
|
javac MyClass.java
java -cp . MyClass |
... and see if your idea was correct.
kind regards,
Jos
|
|

03-20-2010, 11:40 AM
|
|
Member
|
|
Join Date: Mar 2010
Posts: 5
Rep Power: 0
|
|
|
Not sure if this is right but i think i went a different way.
I assigned each letter in the string to a char using charAt(x); but am wanting to now assign the characters in each of those, append them to a string.
The values have been read into the char's correctly, guess I just need this last part to append all the letters to one string, is that a hard thing to do?
|
|

03-20-2010, 11:58 AM
|
|
Member
|
|
Join Date: Mar 2010
Posts: 5
Rep Power: 0
|
|
StringBuilder (Java Platform SE 6)
This isn't really helpful to be as since i'm new i don't understand how to put these instructions into a working example, sorry.
What I thought i could do is something like this:
if the numbers/letters are assigned from their character positions, example
a = number.charAt(5);
b = number.charAt(4);
c = number.charAt(3); ...and so on,
I can use printf and say the string backwards is %c%c%c then insert the arguments, a,b,c but i just want to append the above abc to a string like 'result'.
Could you explain how this can be done by using some kind of a concat method or something?
|
|

03-20-2010, 12:18 PM
|
|
Senior Member
|
|
Join Date: Mar 2010
Posts: 605
Rep Power: 1
|
|
JosAH has given you a skeleton method above. What I sent should have been a link to the append() method of the StringBuilder class (but I see the link doesn't work well in my browser). You invoke it like this:
-Gary-
|
|

03-20-2010, 12:35 PM
|
|
Senior Member
|
|
Join Date: Sep 2008
Location: Voorschoten, the Netherlands
Posts: 3,322
Rep Power: 5
|
|
Originally Posted by cysquatch
|
StringBuilder (Java Platform SE 6)
This isn't really helpful to be as since i'm new i don't understand how to put these instructions into a working example, sorry.
|
Being new to programming doesn't imply you're illiterate does it? Why don't you read what such a StringBuilder can do for you? Spoiler: it can reverse its content (a representation of a String) for you.
kind regards,
Jos
|
|

03-20-2010, 09:01 PM
|
|
Senior Member
|
|
Join Date: Feb 2009
Posts: 661
Rep Power: 2
|
|
|
If you find it hard to connect the dots from the API documentation to a working example (eg because the syntax of Java is new or whatever) then Google is your friend. The first hit for "StringBuilder reverse" looks pretty good when I tried it.
Of course you have to exercise a certain amount of sense in choosing whether to accept what you read. I was going to say common sense but, to be fair, it isn't common when you're starting. So look for usage examples that are simple, easy to follow and presented on pages with professional layout and language use.
Understand any code ideas you pick up. That will involve your textbook/teacher/whatever. And, having done your own work, ask here (with code attempt) if things aren't clear or don't give the intended results.
|
|

03-21-2010, 07:39 AM
|
|
Senior Member
|
|
Join Date: Feb 2010
Location: Ljubljana, Slovenia
Posts: 302
Rep Power: 1
|
|
Now now, let's all take a breather first 
Ok, instead of jumping the gun and stare at complicated classes, why don't we try and figure out a simple solution, for such a simple problem? A few lines of recursive code solve this quite nicely, I'll just describe the solution, so the OP can figure out for himself how to put it into code.
So, to reverse a string, you need to take the first letter, and make it last, and so on and so on. One letter doesn't need to be reversed, so that can be our base case. In short, get the reversed substring without the first character, and append the first character to the back of it. Only substring() and charAt() methods are used and the whole program consists of 3 lines (if you're not counting method headers and main  ).
|
|

03-22-2010, 10:48 AM
|
 |
Senior Member
|
|
Join Date: Aug 2009
Posts: 125
Rep Power: 0
|
|
|
Quote:
|
|
without using any loops or existing methods?
|
y r we forgetting this.. rite in the first post.
|
|

03-22-2010, 01:07 PM
|
|
Senior Member
|
|
Join Date: Feb 2010
Location: Ljubljana, Slovenia
Posts: 302
Rep Power: 1
|
|
|
I was proposing a recursive method, so no loops, and no existing methods probably means the reverse() method from the string class.
|
|

03-22-2010, 01:30 PM
|
|
Senior Member
|
|
Join Date: Sep 2008
Location: Voorschoten, the Netherlands
Posts: 3,322
Rep Power: 5
|
|
Originally Posted by m00nchile
|
|
I was proposing a recursive method, so no loops, and no existing methods probably means the reverse() method from the string class.
|
The String class doesn't have a reverse() method and you'd have to at least use one other method in your recursive solution so it is not allowed ;-)
kind regards,
Jos
|
|

03-23-2010, 02:31 AM
|
|
Member
|
|
Join Date: Mar 2010
Posts: 21
Rep Power: 0
|
|
|
May I ask why you need not to use loops and such? That would be by far the easiest way to reverse a string.
|
|
| Thread Tools |
|
|
| Display Modes |
Linear Mode
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
All times are GMT +2. The time now is 09:17 PM.
|
|