day 3: 28-first-occurrence-of-substring

This commit is contained in:
Kaushik Narayan R 2023-09-28 08:30:59 -07:00
parent 9da326288a
commit 688a352dbe
3 changed files with 37 additions and 0 deletions

View File

@ -0,0 +1,10 @@
#include "soln.hpp"
#include <iostream>
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;
}

View File

@ -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;
}

View File

@ -0,0 +1,9 @@
#include <string>
using namespace std;
class Solution
{
public:
int strStr(string haystack, string needle);
bool test(string haystack, string needle, int answer);
};