Results 1 to 16 of 16
Thread: Reverse a string?
- 03-20-2010, 10:44 AM #1
Member
- Join Date
- Mar 2010
- Posts
- 5
- Rep Power
- 0
- 03-20-2010, 10:49 AM #2
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,606
- Blog Entries
- 7
- Rep Power
- 17
- 03-20-2010, 11:16 AM #3
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 :confused:
- 03-20-2010, 11:20 AM #4
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 #5
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,606
- Blog Entries
- 7
- Rep Power
- 17
Why don't you give it a try:
compile and run it:Java Code:public class MyClass { public static void main(String[] args) { StringBuilder sb= new StringBuilder(); // your code goes here } }
... and see if your idea was correct.Java Code:javac MyClass.java java -cp . MyClass
kind regards,
Jos
- 03-20-2010, 11:40 AM #6
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:45 AM #7
Senior Member
- Join Date
- Mar 2010
- Posts
- 953
- Rep Power
- 4
StringBuilder (Java Platform SE 6)
No, it's not a hard thing to do.
-Gary-
- 03-20-2010, 11:58 AM #8
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 #9
Senior Member
- Join Date
- Mar 2010
- Posts
- 953
- Rep Power
- 4
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-Java Code:sb.append(c);
- 03-20-2010, 12:35 PM #10
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,606
- Blog Entries
- 7
- Rep Power
- 17
- 03-20-2010, 09:01 PM #11
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,561
- Rep Power
- 11
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 #12
Senior Member
- Join Date
- Feb 2010
- Location
- Ljubljana, Slovenia
- Posts
- 470
- Rep Power
- 4
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 :D).
- 03-22-2010, 10:48 AM #13
y r we forgetting this.. rite in the first post. :pwithout using any loops or existing methods?
- 03-22-2010, 01:07 PM #14
Senior Member
- Join Date
- Feb 2010
- Location
- Ljubljana, Slovenia
- Posts
- 470
- Rep Power
- 4
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 #15
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,606
- Blog Entries
- 7
- Rep Power
- 17
- 03-23-2010, 02:31 AM #16
Member
- Join Date
- Mar 2010
- Posts
- 21
- Rep Power
- 0
Similar Threads
-
reverse string split
By Fittersman in forum Advanced JavaReplies: 4Last Post: 03-09-2010, 12:29 AM -
reverse a string with a while loop...
By OptimusPrime in forum New To JavaReplies: 9Last Post: 12-28-2009, 11:06 PM -
Reverse a string not using the substring method
By kathyla18 in forum New To JavaReplies: 17Last Post: 04-08-2009, 04:08 AM -
Reverse and Replace a String in Linear Time
By colin.cruise in forum New To JavaReplies: 5Last Post: 07-01-2008, 09:02 PM -
How to reverse two dimensional
By masaka in forum New To JavaReplies: 4Last Post: 05-19-2008, 10:02 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks