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

New Topic/Question
Reply



MultiQuote




|