Results 1 to 11 of 11
- 03-08-2012, 10:17 PM #1
Senior Member
- Join Date
- Mar 2009
- Posts
- 126
- Rep Power
- 0
- 03-08-2012, 10:38 PM #2
Re: how to reverse string without using string reverse or array ?
Your question is unclear, are you saying you are also prohibited from using an array?
Thats fine if so, because you don't need one. Do you know what string concatenation is, and how to use a 'for' loop?
- 03-08-2012, 10:50 PM #3
Senior Member
- Join Date
- Mar 2009
- Posts
- 126
- Rep Power
- 0
Re: how to reverse string without using string reverse or array ?
- 03-08-2012, 10:56 PM #4
Re: how to reverse string without using string reverse or array ?
"yes of course i know to use for loop"
Ok, great! Then the problem should be trivial. You are also aware that java does string concatenation implicitly as well, such as:
You now have the tool needed to combine any two strings. We also know that loops let us do tasks many times over, say, for every character in a 100 character string. If I wanted to make the above example read 'ba' instead of 'ab' (reversed), then I need only change the order in which I concat them.Java Code:String alpha = "a"; String beta = "b"; String combo = alpha + beta; assert combo.equals("ab") == true;
Now, one last part. We can do this with whole strings, but what about individual characters? Well, take a look at the charAt() method of the string class. Combine this with a loop, a temporary String variable, implicit concatenation, and implicit casting from char to String, and you have your solution. Give it a try and post what you come up with; we can discuss it more!Java Code:String alpha = "a"; String beta = "b"; String combo = beta + alpha; assert combo.equals("ba") == true;
- 03-08-2012, 11:27 PM #5
Senior Member
- Join Date
- Mar 2009
- Posts
- 126
- Rep Power
- 0
Re: how to reverse string without using string reverse or array ?
- 03-09-2012, 04:02 AM #6
Re: how to reverse string without using string reverse or array ?
No problem. Feel free to post what you come up with, and I'll gladly help you further if you need it!
- 03-09-2012, 06:27 AM #7
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,606
- Blog Entries
- 7
- Rep Power
- 17
Re: how to reverse string without using string reverse or array ?
Think recursion:
1) an empty String has itself as its reverse;
2) otherwise a String s can be written as ht where h is a single character and t is the original String s with its first character stripped; the reverse of this String ht is the reverse of t with character h appended.
kind regards,
JosLast edited by JosAH; 03-09-2012 at 07:43 AM.
When people rob a bank they get a penalty; when banks rob people they get a bonus.
- 03-09-2012, 09:40 AM #8
Moderator
- Join Date
- Apr 2009
- Posts
- 10,484
- Rep Power
- 16
- 03-09-2012, 04:29 PM #9
Re: how to reverse string without using string reverse or array ?
Yeah yeah, I know :D
I was trying to make it painfully obvious what was happening. I'm never that explicit in my integration tests, I just didn't want any confusion for someone who's skill level isn't known :)
- 03-15-2012, 08:22 AM #10
Senior Member
- Join Date
- Mar 2009
- Posts
- 126
- Rep Power
- 0
Re: how to reverse string without using string reverse or array ?
Hi quad64bit , sorry for my late reply , here this is what i tried , but i slightly copied from some resource , but the one thing i wanted to know , that is casting
Java Code:I TIRED WITH THE HELP OF OTHER RESOURCE --------------------------------------------- public class ReverseString{ public static void main(String args[]) { String s = "Helloworld"; StringBuilder sb = new StringBuilder(); for(int i = s.length() - 1; i >= 0; --i) sb.append(s.charAt(i)); System.out.println(sb.toString()); } }Here the above second one is mine and it is not giving right answer , here i don't how to cast the int to string :( . please help me bro . And what is "assert" ?what is the use of "assert " in our programJava Code:I TRIED ON YOUR OWN USING YOUR IDEA ---------------------------------------- public class ReverseString{ public static void main(String args[]) { String s = "Helloworld"; for(int i = s.length() - 1; i >= 0; --i) { s.charAt(i); System.out.print((char)i); } } }
Thank you very much for your reply Josh , sorry for the late reply ,
Can you please explain me in detail of the two point which you have given above ? . thank you Josh
- 03-15-2012, 10:46 AM #11
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,606
- Blog Entries
- 7
- Rep Power
- 17
Re: how to reverse string without using string reverse or array ?
There isn't much more detail to supply; if a String is empty, the reversed of the String is the empty String; otherwise reverse the String minus the first character and append the first character to the reverse of the String. One more step would be spoonfeeding; you do it.
kind regards,
JosWhen people rob a bank they get a penalty; when banks rob people they get a bonus.
Similar Threads
-
reverse string
By mallikanala in forum New To JavaReplies: 1Last Post: 01-21-2012, 03:40 PM -
How to reverse a string?
By Neeer in forum New To JavaReplies: 17Last Post: 03-27-2011, 08:10 PM -
Reverse A String Without Allocating A New String
By marco.c84 in forum Advanced JavaReplies: 10Last Post: 03-22-2011, 05:39 AM -
How to reverse a string using while loops?
By JavaS in forum New To JavaReplies: 6Last Post: 03-08-2011, 03:01 AM -
Reverse a string?
By cysquatch in forum New To JavaReplies: 15Last Post: 03-23-2010, 02:31 AM


1Likes
LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks