Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Language:C++ Two useful functions that are not provided in the standard library

ID: 3673635 • Letter: L

Question

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:

Other examples which can help to catch common coding errors:

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:

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:

Also, allow the caller to use multiple *wildcards in the same string. (This assumes the previous option is also done.) For example:

That's it!

Explanation / Answer

Functions are tested with all the given inputs multiple times. They worked perfectly!

#include <bits/stdc++.h>
using namespace std;

int charsearch(string src, char ch, int start = 0, bool ignoreCase = false) {
string searchInto = src;
if (ignoreCase) {
transform(src.begin(), src.end(), searchInto.begin(), ::tolower);
}

int n = searchInto.size();
for (int i = start; i < n; ++i) {
if (searchInto[i] == ch) {
return i;
}
}

return -1;
}

int substr(string src, string tosearch, int start = 0, bool ignoreCase = false) {
string searchInto = src;
if (ignoreCase) {
transform(src.begin(), src.end(), searchInto.begin(), ::tolower);
}

int n = searchInto.size();
int m = tosearch.size();
int ans = -1;
for (int i = start; i < n; ++i) {
int p = i;
int q = 0;

bool escape = false;
while (p < n && q < m) {
if (searchInto[p] == tosearch[q]) {
if (tosearch[q] == '/' && !escape) {
++q;
} else {
++p; ++q;
}
escape = false;
} else if (!escape && tosearch[q] == '*') {
++q;
while (q < m && p < n && searchInto[p] != tosearch[q]) ++p;
escape = false;
} else if (!escape && tosearch[q] == '?') {
++p; ++q;
escape = false;
} else if (tosearch[q] == '/' && !escape) {
escape = true;
++q;
} else break;
}

if (q == m) {
return i;
}

if (q == m - 1 && tosearch[q] == '*') {
if (q > 0 && tosearch[q - 1] == '/') continue;
else return i;
}
}

return -1;
}

int main() {

   while (true) {
       string searchInto, tosearch;
       getline(cin, searchInto);
       getline(cin, tosearch);
       cout << substr(searchInto, tosearch) << endl;
   }

   return 0;
}