Question: Write a function str_search that will take 2 strings, pattern and text as parameters and will return the number of occurrences of pattern in text.

Write a function str_search that will take 2 strings, pattern and text as parameters and will return the number of occurrences of pattern in text. For example, if pattern is "ana" and text is "ana ate the banana", then the resulting value should be 3. Consider that matching is always case-sensitive. unsigned int str_search(const char *pattern, const char *text);

Must be in C++ and only include #incluse and or

Only can have the function. If you want to write a main to test it some test cases are

  • pattern = "ana", text = "ana ate the banana" ( should return 3) (Be careful with words like "banana" where the word contains the pattern multiple times, but shares letters with a previous occurrence)
  • pattern = "aa", text = "ana ate the banana"; (should return 0)
  • empty string, return 0
  • pattern = "ana ate the banana" , text = "ana ate the banana" (should return 1)
  • pattern = "na", text = "ana ate the banana" (should return 3)
  • pattern = " " (1 single space), text = "ana ate the banana" (should return 3)
  • pattern = "a a", text = "ana ate the banana" (should return 1)

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!