Can anyone explain this logic?
This part of the program takes a string and print each letter on a single line, while speaking the string backwards.
To termination the For loop in the printBackwards method it's n>= 0. I originally thought it would be n==0, but using n==0 the compiler just terminates the program. I was only able to get it to work through trial and error (n>=0).
Code:
public static void main(String[] args) {
String word = "Backpack";
printBackwards(word);
}
public static void printBackwards (String s){
for ( int n = length(s) - 1; n >= 0; n--){
System.out.println(s.charAt(n));
}
}
Output:
Re: Can anyone explain this logic?
No response from OP for a long time.
Re: Can anyone explain this logic?
No, I'm still here. Didn't have internet for awhile.
Thanks for the explanation, it really helped.