Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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 5. 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 I****** Elba", the function should output 3. If the input is "Peanuts", thefunction should return 0.
Approach: Let's assume the signature of our function is countAsterisks (s) where s is the input string. In the body of your function, check if s[0] is equal to an asterisk. If the string contains only a single character and it is an asterisk, return 1. If the string contains only a single character and it is not an asterisk, return 0. 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 s1 that is the same as the input string s but without the first character. Recursively call countAsterisks (s1) and return the value returned from this recursive call, plus one (if there is an asterisk in s[0]).
Also, writing any explicit loop in your code results a 0 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 (n) for when n is initially "P***** 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.
\table[[call#,s,\table[[value returned to this],[call]]],[1,"P**nut",3],[2,"*",3]]
image text in transcribed

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

SQL Database Programming

Authors: Chris Fehily

1st Edition

1937842312, 978-1937842314

More Books

Students also viewed these Databases questions