Program for length of a string using recursion Last Updated : 18 Mar, 2025 Comments Improve Suggest changes Like Article Like Report Given a string calculate length of the string using recursion. Examples: Input: str = "abcd"Output: 4Explanation: The string "abcd" has a length of 4.Input: str = "GEEKSFORGEEKS"Output: 13We have also covered this topic—feel free to check it out as well5 Different methods to find length of a string in C++ How to find length of a string without string.h and loop in C?Approach:If the string is empty (""), the function returns 0 since there are no characters left to count. Otherwise, the first character is removed using substring(1), reducing the string size by one. The function then recursively calls itself on the remaining substring. With each recursive call, 1 is added to the result to account for the removed character. This process continues until the string becomes empty, at which point the recursion stops. C++ #include <bits/stdc++.h> using namespace std; int recLen(string str) { // Base case: If the string is empty if (str == "") return 0; return 1 + recLen(str.substr(1)); } // Driver code int main() { string str = "GeeksforGeeks"; cout << recLen(str) << endl; return 0; } Java import java.util.*; public class GfG { static int recLen(String str) { if (str.equals("")) return 0; else return recLen(str.substring(1)) + 1; } /* Driver program to test above function */ public static void main(String[] args) { String str = "GeeksforGeeks"; System.out.println(recLen(str)); } } Python def recLen(str): # Base case: If the string is empty if str == "": return 0 return 1 + recLen(str[1:]) # Driver code str = "GeeksforGeeks" print(recLen(str)) C# using System; class GfG { static int recLen(string str) { // Base case: If the string is empty if (str == "") return 0; return 1 + recLen(str.Substring(1)); } static void Main() { string str = "GeeksforGeeks"; Console.WriteLine(recLen(str)); } } JavaScript function recLen(str) { // Base case: If the string is empty if (str === "") return 0; return 1 + recLen(str.substring(1)); } // Driver code let str = "GeeksforGeeks"; console.log(recLen(str)); // Output: 13 Output13 Time Complexity: O(n2), where n is the length of the string.Auxiliary Space: O(n2)Note: This approach is inefficient as it requires extra space, incurs function call overhead, and runs in O(n²) time, making it impractical for large inputs. Comment More infoAdvertise with us Next Article Program for length of a string using recursion S Shahnawaz_Ali Follow Improve Article Tags : Misc Strings Recursion DSA Practice Tags : MiscRecursionStrings Similar Reads Introduction to Recursion The process in which a function calls itself directly or indirectly is called recursion and the corresponding function is called a recursive function. A recursive algorithm takes one step toward solution and then recursively call itself to further move. The algorithm stops once we reach the solution 14 min read What is Recursion? Recursion is defined as a process which calls itself directly or indirectly and the corresponding function is called a recursive function.Example 1 : Sum of Natural Numbers Let us consider a problem to find the sum of natural numbers, there are several ways of doing that but the simplest approach is 8 min read Difference between Recursion and Iteration A program is called recursive when an entity calls itself. A program is called iterative when there is a loop (or repetition).Example: Program to find the factorial of a number C++ // C++ program to find factorial of given number #include<bits/stdc++.h> using namespace std; // ----- Recursion 6 min read Types of Recursions What is Recursion? The process in which a function calls itself directly or indirectly is called recursion and the corresponding function is called a recursive function. Using recursive algorithm, certain problems can be solved quite easily. Examples of such problems are Towers of Hanoi (TOH), Inord 15+ min read Finite and Infinite Recursion with examples The process in which a function calls itself directly or indirectly is called Recursion and the corresponding function is called a Recursive function. Using Recursion, certain problems can be solved quite easily. Examples of such problems are Towers of Hanoi (TOH), Inorder/Preorder/Postorder Tree Tr 6 min read What is Tail Recursion Tail recursion is defined as a recursive function in which the recursive call is the last statement that is executed by the function. So basically nothing is left to execute after the recursion call.For example the following function print() is tail recursive.C++// An example of tail recursive funct 7 min read What is Implicit recursion? What is Recursion? Recursion is a programming approach where a function repeats an action by calling itself, either directly or indirectly. This enables the function to continue performing the action until a particular condition is satisfied, such as when a particular value is reached or another con 5 min read Why is Tail Recursion optimization faster than normal Recursion? What is tail recursion? Tail recursion is defined as a recursive function in which the recursive call is the last statement that is executed by the function. So basically nothing is left to execute after the recursion call. What is non-tail recursion? Non-tail or head recursion is defined as a recur 4 min read Recursive Functions A Recursive function can be defined as a routine that calls itself directly or indirectly. In other words, a recursive function is a function that solves a problem by solving smaller instances of the same problem. This technique is commonly used in programming to solve problems that can be broken do 4 min read Difference Between Recursion and Induction Recursion and induction are fundamental ideas in computer science and mathematics that might be regularly used to solve problems regarding repetitive structures. Recursion is a programming technique in which a function calls itself to solve the problem, whilst induction is a mathematical proof techn 4 min read Like