Created
December 1, 2025 22:48
-
-
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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