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 1 *** Elba", the function should output 3. If the input is "Peanuts", the function 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. call# S value returned to this call 1 2 "P**nut*" “** *nut*" 3 3

Database System Concepts
7th Edition
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Chapter1: Introduction
Section: Chapter Questions
Problem 1PE
icon
Related questions
Question

Please help me with these question. I am having trouble understanding what to do. Please use HTML, CSS, and JavaScript

Thank you

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
1 *** Elba", the function should output 3. If the input is "Peanuts", the function 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.
call#
S
value returned to this
call
1
2
"P**nut*"
“**
*nut*"
3
3
Transcribed Image Text: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 1 *** Elba", the function should output 3. If the input is "Peanuts", the function 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. call# S value returned to this call 1 2 "P**nut*" “** *nut*" 3 3
Expert Solution
steps

Step by step

Solved in 2 steps

Blurred answer
Similar questions
  • SEE MORE QUESTIONS
Recommended textbooks for you
Database System Concepts
Database System Concepts
Computer Science
ISBN:
9780078022159
Author:
Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:
McGraw-Hill Education
Starting Out with Python (4th Edition)
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
Digital Fundamentals (11th Edition)
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
C How to Program (8th Edition)
C How to Program (8th Edition)
Computer Science
ISBN:
9780133976892
Author:
Paul J. Deitel, Harvey Deitel
Publisher:
PEARSON
Database Systems: Design, Implementation, & Manag…
Database Systems: Design, Implementation, & Manag…
Computer Science
ISBN:
9781337627900
Author:
Carlos Coronel, Steven Morris
Publisher:
Cengage Learning
Programmable Logic Controllers
Programmable Logic Controllers
Computer Science
ISBN:
9780073373843
Author:
Frank D. Petruzella
Publisher:
McGraw-Hill Education