Reverse string character's order

Page 1 of 1

3 Replies - 7669 Views - Last Post: 17 February 2009 - 04:04 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=87282&amp;s=e33fbcc23cf48a7a57956ff49e28e616&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

#1 lukbut_1

Reputation: 1
• Posts: 10
• Joined: 05-January 09

Reverse string character's order

Posted 17 February 2009 - 12:50 PM

Hi! Yesterday we covered how one can use recursive methods to make a string, say 'John', be spelled in reverse i.e. 'nhoJ'. The code was given to us by the professor, but I don't fully understand it. Here's the code of the method to do this:

```public static void reverse2(String s){
if (s.length() > 0){
System.out.println(s.charAt(s.length()-1));
reverse2(s.substring(0, s.length() -1));
}

}
```

Basically, I see it working in this way:
If the length of the string is more than 0, the last character of the string is printed out (-1 used since the index of a character in a string is always a number less than the actual number of characters). The method is then used recursively to print out the other letters in the string. How is this done? I know that substring enables one to select only part of the string in question, but if one selects from 0 to s.length() -1, isn't one selecting the whole string again?

Thanks

Is This A Good Question/Topic? 0

Replies To: Reverse string character's order

#2 NickDMax

Reputation: 2255
• Posts: 9,245
• Joined: 18-February 07

Re: Reverse string character's order

Posted 17 February 2009 - 01:11 PM

well no... the substring method is:

String.substring(int begin, int length)

so if you did string.substring(0, string.length) you would select the whole string... but string.substring(0, string.length - 1) selects all but the last char.

Take the string "Hello World" -- has a legth of 11, the first char has a index of 0 the last an index of 10...

"Hello World".substring(0, 10) return "Hello Worl" which is a string with length 10, and H has an index of 0 and the last char has an index of 9

so you seem to be confusing indexing with counting... counting begins with 1 and indexing (in java) begins with 0.

#3 5thWall

• Occasional Member

Reputation: 31
• Posts: 530
• Joined: 17-September 08

Re: Reverse string character's order

Posted 17 February 2009 - 02:49 PM

According to the Java API substring(int,int) returns the string between the first index (inclusive) and the second index (exclusive)

http://java.sun.com/...l#substring(int, int)

Edit: Stupid link is stupid because it contains spaces. Try http://java.sun.com/...ang/String.html

This post has been edited by 5thWall: 17 February 2009 - 02:54 PM

#4 NickDMax

Reputation: 2255
• Posts: 9,245
• Joined: 18-February 07

Re: Reverse string character's order

Posted 17 February 2009 - 04:04 PM

Aye, I stand corrected. The second number is the second index (exclusive) (meaning it does not include that index in the resulting substring.)

So "Hello World".substring(0, 10) so 'd' is the char in the 10'th position and we leave that one off (exclude it).

Sorry about that. you are not confusing indexing and counting, *we* just did read the javadoc carefully. Thanks 5thWall!