Results 1 to 11 of 11
- 03-08-2012, 11:17 PM #1
Senior Member
- Join Date
- Mar 2009
- Posts
- 126
- Rep Power
- 0
- 03-08-2012, 11: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, 11: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, 11: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:
Java Code:String alpha = "a"; String beta = "b"; String combo = alpha + beta; assert combo.equals("ab") == true;
Java Code:String alpha = "a"; String beta = "b"; String combo = beta + alpha; assert combo.equals("ba") == true;
- 03-09-2012, 12:27 AM #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, 05: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, 07:27 AM #7
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 14,422
- Blog Entries
- 7
- Rep Power
- 28
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 08:43 AM.
Build a wall around Donald Trump; I'll pay for it.
- 03-09-2012, 10:40 AM #8
Moderator
- Join Date
- Apr 2009
- Posts
- 13,541
- Rep Power
- 26
- 03-09-2012, 05: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, 09: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()); } }
Java 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, 11:46 AM #11
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 14,422
- Blog Entries
- 7
- Rep Power
- 28
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,
JosBuild a wall around Donald Trump; I'll pay for it.
Similar Threads
-
reverse string
By mallikanala in forum New To JavaReplies: 1Last Post: 01-21-2012, 04:40 PM -
How to reverse a string?
By Neeer in forum New To JavaReplies: 17Last Post: 03-27-2011, 09:10 PM -
Reverse A String Without Allocating A New String
By marco.c84 in forum Advanced JavaReplies: 10Last Post: 03-22-2011, 06:39 AM -
How to reverse a string using while loops?
By JavaS in forum New To JavaReplies: 6Last Post: 03-08-2011, 04:01 AM -
Reverse a string?
By cysquatch in forum New To JavaReplies: 15Last Post: 03-23-2010, 03:31 AM
Bookmarks