I am trying to find the length of the string with out using the inbuilt len()
function. Here's my python code:
increment = -1
def lenRecur(aStr):
'''
aStr: a string
returns: int, the length of aStr
'''
global increment
if aStr == '':
return increment
else:
increment += 1
return lenRecur (aStr[increment:])
print lenRecur ("abcdefq")
The result I am expecting is 7 but what I got was 4.What I realized was when the increment
became 2, the value pass to the lenRecur (aStr[increment:])
was "defq"
. Which means aStr[2:]
is evaluated as "defq"
instead of "cdefq"
.
Why this is happening?