diff --git a/28-first-occurrence-of-substring/driver.cpp b/28-first-occurrence-of-substring/driver.cpp new file mode 100644 index 0000000..5fe327b --- /dev/null +++ b/28-first-occurrence-of-substring/driver.cpp @@ -0,0 +1,10 @@ +#include "soln.hpp" +#include + +int main() { + string haystack = "Drink some water, reload your mags, and let's get back out there."; + string needle = "water"; + Solution soln; + cout << "Found correctly? " << soln.test(haystack, needle, 11) << endl; + return 0; +} \ No newline at end of file diff --git a/28-first-occurrence-of-substring/soln.cpp b/28-first-occurrence-of-substring/soln.cpp new file mode 100644 index 0000000..a507b48 --- /dev/null +++ b/28-first-occurrence-of-substring/soln.cpp @@ -0,0 +1,18 @@ +#include "soln.hpp" + +int Solution::strStr(string haystack, string needle) +{ + int index = haystack.find(needle); + if (index != std::string::npos) + { + return index; + } + else + { + return -1; + } +} + +bool Solution::test(string haystack, string needle, int answer) { + return strStr(haystack, needle) == answer; +} \ No newline at end of file diff --git a/28-first-occurrence-of-substring/soln.hpp b/28-first-occurrence-of-substring/soln.hpp new file mode 100644 index 0000000..e9d5fe5 --- /dev/null +++ b/28-first-occurrence-of-substring/soln.hpp @@ -0,0 +1,9 @@ +#include + +using namespace std; +class Solution +{ +public: + int strStr(string haystack, string needle); + bool test(string haystack, string needle, int answer); +}; \ No newline at end of file