This C++ assignment asks to write a function that determines if a C-string begins with a...

60.1K

Verified Solution

Question

Programming

This C++ assignment asks to write a function that determines ifa C-string begins with a specified prefix. It should have thefollowing signature:

 bool starts(char *str, char *prefix) 

It should return true if str begins withprefix, false if not. It should return false ifprefix is longer than str. Seethe table below for some examples of what your function shouldreturn for various cases:

strprefixreturns
airplanesairtrue
airplanesabcfalse
airplanesplanefalse
airplanesairplanetrue
airairplanefalse


Assume that the pointers passed to it are pointers to valid,null-terminated C-Strings. If you wish, you can use the strlen()and strcmp() functions, but it isn't required.

Hint: it can make things easier to check if length ofprefix > length of str. If so,you can immediately return false! If not, you can continue on withyour check.

Answer & Explanation Solved by verified expert
3.7 Ratings (314 Votes)
include include using namespace    See Answer
Get Answers to Unlimited Questions

Join us to gain access to millions of questions and expert answers. Enjoy exclusive benefits tailored just for you!

Membership Benefits:
  • Unlimited Question Access with detailed Answers
  • Zin AI - 3 Million Words
  • 10 Dall-E 3 Images
  • 20 Plot Generations
  • Conversation with Dialogue Memory
  • No Ads, Ever!
  • Access to Our Best AI Platform: Flex AI - Your personal assistant for all your inquiries!
Become a Member

Other questions asked by students