Skip to content

Instantly share code, notes, and snippets.

@Bravo6nteringtheroom
Created December 1, 2025 22:48
Show Gist options
  • Select an option

  • Save Bravo6nteringtheroom/74ae409c5fc95b7a1ad3e9e680774b1a to your computer and use it in GitHub Desktop.

Select an option

Save Bravo6nteringtheroom/74ae409c5fc95b7a1ad3e9e680774b1a to your computer and use it in GitHub Desktop.
Yeah guys sorry i know i used try-catch in order to do this but it was just to solve the index out of bounds error any suggestion how to deal with that But to be honest it really helped me understand more about recursion and who knows maybe i'll do the same for other functions
public class RecursiveStringLength {
static int len(String word, int n) {
try {
if(word.charAt(n) == ' ') return n;
return len(word, n+1);
} catch(IndexOutOfBoundsException e) {
return n;
}
}
public static void main(String[] args) {
System.out.println(len("hello", 0)); // 5
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment