# Program for length of a string using recursion

Given a string calculate length of the string using recursion.

Examples:

```Input : str = "abcd"
Output :4

Input : str = "GEEKSFORGEEKS"
Output :13
```

## Recommended: Please try your approach on {IDE} first, before moving on to the solution.

Algorithm

```recLen(str)
{
If str is NULL
return 0
Else
return 1 + recLen(str + 1)
}```

## C++

 `// CPP program to calculate length of  ` `// a string using recursion ` `#include ` `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

 `// java program to calculate length of  ` `// a string using recursion ` `import` `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. `

## Python3

 `# Python program to calculate  ` `# length of a string using  ` `# recursion ` `str` `=` `"GeeksforGeeks"` ` `  `# Function to  ` `# calculate length ` `def` `string_length(``str``) : ` `     `  `    ``# if we reach at the ` `    ``# end of the string ` `    ``if` `str` `=``=` `'': ` `        ``return` `0` `    ``else` `: ` `        ``return` `1` `+` `string_length(``str``[``1``:])  ` `     `  `# Driver Code ` `print` `(string_length(``str``)) ` ` `  `# This code is contributed by ` `# Prabhjeet `

## C#

 `// C# program to calculate length of  ` `// a string using recursion ` `using` `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

 ` `

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.

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.

My Personal Notes arrow_drop_up Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

Article Tags :
Practice Tags :

4

Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.