Results 1 to 18 of 18
Thread: How to reverse a string?
- 03-25-2011, 02:34 PM #1
Member
- Join Date
- Mar 2011
- Posts
- 6
- Rep Power
- 0
- 03-25-2011, 02:40 PM #2
Member
- Join Date
- Mar 2011
- Location
- chennai
- Posts
- 55
- Rep Power
- 0
one way:
take the string as character array
c[]=[h,e, ,i,s, ,g,o,o,d]
now read each character from end concat each character into a string.
String s=new String();
s.concat(c[end]);
second way:
in StringBuffer there is method called reverse()
by using this we can reverse the string
i hope you can understand:rolleyes:
-
here's another way:
Java Code:String str = "he is good"; String[] temp; String reversedStr = ""; String delimiter = " "; temp = str.split(delimiter); // temp [string array] now stores three values, "he", "is", and "good" for (String s:temp) { reversedStr += new StringBuffer(s).reverse().toString() + delimiter; } // reversedStr = "eh si doog";
- 03-25-2011, 03:22 PM #4
Member
- Join Date
- Mar 2011
- Posts
- 6
- Rep Power
- 0
I cannot use array too or any other functions that i did not mention.
-
oh you have to do it manually i see. then you need to work out your logic
- while (index doesnt equal length of string) {
- X = start index;
- if charAt(index) = white space {
index X to here = new word
reverse it
}
- loop }
"length of string"
use String.length
"index X to here"
when you have two indexes e.g. 0 and 2, you can use subsequence to get all the characters between two indexes
"reverse it"
take the last index first and copy it to a temporary variable, loop through the sub-word until the length of it is finishedLast edited by ozzyman; 03-25-2011 at 03:33 PM.
- 03-25-2011, 03:32 PM #6
Member
- Join Date
- Mar 2011
- Posts
- 6
- Rep Power
- 0
Can you give a sample code please...
-
i would except this looks like your homework :). you wont learn unless you try it yourself, so i've given u some logic to help u out. try to write it and try to compile it, if you get an error tell us what the error is
- 03-27-2011, 04:55 AM #8
Member
- Join Date
- Mar 2011
- Location
- Bulacan, Philippines
- Posts
- 23
- Rep Power
- 0
Java Code:String sample="he is good"; for(int x=sample.length()-1;x>=0;x--){ System.out.print(sample.charAt(x)); }Keep Moving Forward
- 03-27-2011, 05:13 AM #9
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,561
- Rep Power
- 11
- 03-27-2011, 05:25 AM #10
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
What have you tried to solve this? You got advice in this thread, some incorrect as pbrock mentioned but they are showing the idea. Show what kind have work you have done and we will let you know what to focus on.
-
- 03-27-2011, 06:19 PM #12
Senior Member
- Join Date
- Mar 2011
- Posts
- 261
- Rep Power
- 3
- 03-27-2011, 07:12 PM #13
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
Please don't spoon feed code. Especially when the code you are giving is incorrect, and has already been provided, and the reason why it was wrong has been explained.
- 03-27-2011, 07:19 PM #14
Please compile, run and test any code before posting. That'll save you from the embarrassment of posting erroneous code that doesn't come close to meeting the requirement.
And when you do manage to develop a code that works, don't spoonfeed -- that deprives the questioner of a learning opportunity.
db
- 03-27-2011, 07:59 PM #15
Senior Member
- Join Date
- Mar 2011
- Posts
- 261
- Rep Power
- 3
- 03-27-2011, 08:02 PM #16
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
the result should be: "eH si doog"
not : "doog si eH"
Also, don't spoonfeed.
-
- 03-27-2011, 08:10 PM #18
Senior Member
- Join Date
- Mar 2011
- Posts
- 261
- Rep Power
- 3
Similar Threads
-
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 -
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


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks