-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLeetcode_28.cpp
26 lines (20 loc) · 947 Bytes
/
Leetcode_28.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#include <iostream> // Include the input-output stream library
#include <string> // Include the string library
using namespace std; // Use the standard namespace to avoid prefixing std::
// Define a class named Solution
class Solution {
public:
// Function to find the first occurrence of "needle" in "haystack"
int strStr(string haystack, string needle) {
return haystack.find(needle); // Using the find() function of the string class
}
};
int main()
{
string haystack = "hello"; // Define the main string where we search
string needle = "ll"; // Define the substring to be found
Solution a; // Create an instance of the Solution class
int result = a.strStr(haystack, needle); // Call strStr() and store the result
cout << result << endl; // Print the result (index of first occurrence or -1 if not found)
return 0; // Indicate successful program termination
}