Question: e . Problem 5 . Devise a function that receives a string and computes how many asterisks are in the string with a recursive method.
e Problem Devise a function that receives a string and computes how many asterisks are in the string with a recursive method. For instance, if the input is "Able was I ere Elba", the function should output If the input is "Peanuts", thefunction should return
Approach: Let's assume the signature of our function is countAsterisks where is the input string. In the body of your function, check if is equal to an asterisk. If the string contains only a single character and it is an asterisk, return If the string contains only a single character and it is not an asterisk, return If the string is longer than one character, determine if the first character is an asterisk and count it if this is the case. Then, create a new string that is the same as the input string but without the first character. Recursively call countAsterisks s and return the value returned from this recursive call, plus one if there is an asterisk in s
Also, writing any explicit loop in your code results a for this question. As ever, don't forget to provide pre and post conditions.
Now copy the trace table on the next page in your word file and trace your function countAsterisks for when is initially nut As a guideline, we have populated the tables with the values for the first calls and the corresponding returned values for each of those calls.
tablecall#stablevalue returned to thiscallPnut
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
