...

/

Solution Review: Length of a String

Solution Review: Length of a String

This review provides a detailed analysis of the solution to find the length of a string recursively.

We'll cover the following...

Solution: Using Recursion

Press + to interact
def recursiveLength(testVariable) :
# Base Case
if (testVariable == "") :
return 0
# Recursive Case
else :
return 1 + recursiveLength(testVariable[1:])
# Driver Code
testVariable = "Educative"
print (recursiveLength(testVariable))

Explanation

The base case for this solution will test for an empty string "". If the string is empty we return 00.

For the recursive case, we call another instance of the same function, but we remove the first element. When the child element returns the length, we add 11 to the returned length.

The recursive case is based on the paradigm that the length of the complete string is 11 ...