Skip to content

Commit bdb883b

Browse files
feat(recursion-solutions): add string length using recursion
1 parent 2e990b6 commit bdb883b

1 file changed

Lines changed: 22 additions & 0 deletions

File tree

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
/*
2+
PROBLEM:
3+
Find length of string using recursion.
4+
5+
APPROACH:
6+
Remove one character each time and count.
7+
*/
8+
9+
public class StringLength {
10+
11+
public static int length(String str) {
12+
if (str.length() == 0) {
13+
return 0;
14+
}
15+
16+
return 1 + length(str.substring(1));
17+
}
18+
19+
public static void main(String[] args) {
20+
System.out.println(length("hello"));
21+
}
22+
}

0 commit comments

Comments
 (0)