From 688a352dbeb5317b47a6e5be48b1eb5fca496445 Mon Sep 17 00:00:00 2001 From: Kaushik Narayan R Date: Thu, 28 Sep 2023 08:30:59 -0700 Subject: [PATCH] day 3: 28-first-occurrence-of-substring --- 28-first-occurrence-of-substring/driver.cpp | 10 ++++++++++ 28-first-occurrence-of-substring/soln.cpp | 18 ++++++++++++++++++ 28-first-occurrence-of-substring/soln.hpp | 9 +++++++++ 3 files changed, 37 insertions(+) create mode 100644 28-first-occurrence-of-substring/driver.cpp create mode 100644 28-first-occurrence-of-substring/soln.cpp create mode 100644 28-first-occurrence-of-substring/soln.hpp 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