Ask Question
2 November, 19:31

Write a recursive function, len, that accepts a parameter that holds a string value, and returns the number of characters in the string. The length of the string is: 0 if the string is empy ("") 1 more than the length of the string beyond the first character

+2
Answers (1)
  1. 2 November, 23:12
    0
    following are the method definition to this question:

    def len (x) : #defining a method

    if (x=="") : #defining condition that checks value store nothing

    return 0# return value

    else: # else block

    return 1+len (x[1:]) #use recursive function and return value

    print (len ('The collection of table')) #call method and prints its value

    Output:

    23

    Explanation:

    In the above method definition, a "len" method is declared, in which x variable accepts as its parameter, inside the method a condition block is used, which can be explained as follows:

    In if block, we check the value is equal to null if this condition is true, it will return a value, that is 0. In this condition is false, it will go to else block in this block, the recursive method is used, which calculates value length and return its value.
Know the Answer?
Not Sure About the Answer?
Find an answer to your question ✅ “Write a recursive function, len, that accepts a parameter that holds a string value, and returns the number of characters in the string. ...” in 📘 Computers and Technology if you're in doubt about the correctness of the answers or there's no answer, then try to use the smart search and find answers to the similar questions.
Search for Other Answers