I am trying to understand how String length() function work while reversing a string. String length is 4 then why i need to give length()-1 in below working code.
No issue in below code, need help to understand length()
public class MStringReverse {
String getReverse(String input) {
System.out.println(input.length());
String reverse = "";
for(int i = input.length() - 1; i >= 0; i--) {
reverse = reverse + input.charAt(i);
}
return reverse;
}
public static void main(String[] args) {
MStringReverse mr = new MStringReverse();
String result = mr.getReverse("Achilis");
System.out.println(result);
}
}