Question: Language:C++ Two useful functions that are not provided in the standard library are find a char acter in a string and find a substring in
Language:C++
Two useful functions that are not provided in the standard library are find a character in a string and find a substring in a string.
For instance, the character 'e'appears at position 2in the string "The quick brown fox". However, it does not appearin the string "cat's bat about yarn"at all. So our function could return a valid index if the character is found (2above) or -1(an invalid index) when it can't find the character.
Substrings are similar. The string "he"appears at position 1in "The quick brown fox". But, again, it does not appear at all in "cat's bat about yarn". And we can return -1or a valid index (such as 1here) to indicate where the substring was found.
Write these two functions both named find. Place them in a library called strextra(since the name string is already taken).
Some further examples:
string | looking for | returned | which find ------------------------+--------------------+------------+-------------- "The quick brown fox" | 'e' | 2 | char "The quick brown fox" | "e" | 2 | C-string "The quick brown fox" | ' ' | 3 | char "The quick brown fox" | "quick" | 4 | C-string "The quick brown fox" | "quiet" | -1 | C-string
Other examples which can help to catch common coding errors:
string | looking for | returned | which find | | | is called ------------------------+--------------------+------------+-------------- "The quick brown fox" | "cow" | 7 | string "The quick brown fox" | "hix" | 1 | string "11112" | "112" | -1 | string
Next write a test application for this library. A test application is basically a driver that tests all the functions in the library.
Also, allow the caller to decide if the search should be case sensitive or not.
Also, allow the caller of your function to tell you the starting position from which to search their string.
Examples:
string, search_from, find found_at "dumb", 0, 'd' --> 0 "lamb", 0, 'q' --> -1 "pits", 2, 'i' --> -1 "help", 5, 'h' --> -1 "+1.4", 3, "4" --> 3 "take", 0, "ake" --> 1 "lean", 3 'n' --> 3 "3.14", 0 "14" --> 2 "wall", 1 "wa" --> -1\
Also, allow the caller to use a single wildcard. The wildcard characters we'll use are *to match any sequence of (zero or more) characters and ?to match any single character. (Note, to allow them to search for a *or a ?in a string, you must fashion an escape-sequence like we use to print tabs and newlines. I'd recommend the /character.)
Examples:
search_string, find found_at "dumb bunnies", "??mb" --> 0 "lamb", "??mb" --> 0 "lamb", "*mb" --> 0 "lamb", "?mb" --> 1 "pits", "*s" --> 0 "help", "hel?" --> 0 "yellow", "y*l" --> 0 "that's why I like milk", "y*l" --> 9 "What's that?", "/?" --> 11 "pretty *'s", "/*" --> 7 "/home/students", "//" --> 0 "dumb", "*d" --> 0 "dumb", "b*" --> 3
Also, allow the caller to use multiple *wildcards in the same string. (This assumes the previous option is also done.) For example:
search_string, find found_at "dumb bunnies", "b*b*n" --> 3 "dumb bunnies", "b*n*n" --> 3 "dumb bunnies", "u*n*n" --> 1
That's it!
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
