Reverse string character's order

Using recursive method to reverse string order

Page 1 of 1

3 Replies - 6466 Views - Last Post: 17 February 2009 - 04:04 PM Rate Topic: -----

#1 lukbut_1  Icon User is offline

  • New D.I.C Head

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

Reverse string character's order

Post icon  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  Icon User is offline

  • Can grep dead trees!
  • member icon

Reputation: 2250
  • View blog
  • 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.
Was This Post Helpful? 1
  • +
  • -

#3 5thWall  Icon User is offline

  • Occasional Member

Reputation: 31
  • View blog
  • 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. :P Try http://java.sun.com/...ang/String.html

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

Was This Post Helpful? 0
  • +
  • -

#4 NickDMax  Icon User is offline

  • Can grep dead trees!
  • member icon

Reputation: 2250
  • View blog
  • 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!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1