Program for length of a string using recursion

13 Jul, 2022
Given a string calculate length of the string using recursion. Examples:  
Input : str = "abcd"
Output :4

Input : str = "GEEKSFORGEEKS"
Output :13
 
Chapters
descriptions off, selected
captions settings, opens captions settings dialog
captions off, selected
English
This is a modal window.
Beginning of dialog window. Escape will cancel and close the window.
End of dialog window.
We have discussed 5 Different methods to find length of a string in C++ How to find length of a string without string.h and loop in C?Algorithm  
recLen(str)
{
   If str is NULL 
       return 0
   Else 
      return 1 + recLen(str + 1)
}
 
C++
Java
Python3
C#
PHP
Javascript
// CPP program to calculate length of // a string using recursion#include <bits/stdc++.h>using namespace std;  /* Function to calculate length */int recLen(char* str)    {    // if we reach at the end of the string    if (*str == '\0')         return 0;    else        return 1 + recLen(str + 1); }  /* Driver program to test above function */int main(){    char str[] = "GeeksforGeeks";    cout << recLen(str);    return 0;}
// java program to calculate length of // a string using recursionimport java.util.*;  public class GFG{      /* Function to calculate length */    private static int recLen(String str)     {          // if we reach at the end of the string        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));    }}  // This code is contributed by Sam007.
# Python program to calculate # length of a string using # recursionstr = "GeeksforGeeks"  # Function to # calculate lengthdef string_length(str) :          # if we reach at the    # end of the string    if str == '':        return 0    else :        return 1 + string_length(str[1:])       # Driver Codeprint (string_length(str))  # This code is contributed by# Prabhjeet
// C# program to calculate length of // a string using recursionusing System;  public class GFG{      /* Function to calculate length */    private static int recLen(string str)     {          // if we reach at the end of the string        if (str.Equals(""))            return 0;        else            return recLen(str.Substring(1)) + 1;    }      /* Driver program to test above function */    public static void Main()     {                    string str ="GeeksforGeeks";        Console.WriteLine(recLen(str));    }}  // This code is contributed by vt_m.
<?php// PHP program to calculate // length of a string using // recursion   // Function to // calculate lengthfunction recLen(&$str, $i)    {    // if we reach at the    // end of the string    if ($i == strlen($str))         return 0;    else        return 1 +                recLen($str,                       $i + 1); }    // Driver Code$str = "GeeksforGeeks";echo (recLen($str, 0));  // This code is contributed by// Manish Shaw(manishshaw1)?>
<script>  // JavaScript program to calculate length of // a string using recursion  /* Function to calculate length */    function recLen(str)     {            // if we reach at the end of the string        if (str == "")            return 0;        else            return recLen(str.substring(1)) + 1;    }  // Driver code          let str ="GeeksforGeeks";        document.write(recLen(str));             </script>
Output: 
13
Time Complexity : O(n) Auxiliary Space : O(n) for recursion call stack.  
Since this solution requires extra space and function call overhead, it is not recommended to use it in practice. 
manishshaw1
prabhjeetbhabra
susmitakundugoaldanga
Recursion
Strings
Strings
Recursion
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.