File tree Expand file tree Collapse file tree 1 file changed +26
-0
lines changed Expand file tree Collapse file tree 1 file changed +26
-0
lines changed Original file line number Diff line number Diff line change 1+ #include < iostream> // Include the input-output stream library
2+ #include < string> // Include the string library
3+
4+ using namespace std ; // Use the standard namespace to avoid prefixing std::
5+
6+ // Define a class named Solution
7+ class Solution {
8+ public:
9+ // Function to find the first occurrence of "needle" in "haystack"
10+ int strStr (string haystack, string needle) {
11+ return haystack.find (needle); // Using the find() function of the string class
12+ }
13+ };
14+
15+ int main ()
16+ {
17+ string haystack = " hello" ; // Define the main string where we search
18+ string needle = " ll" ; // Define the substring to be found
19+
20+ Solution a; // Create an instance of the Solution class
21+ int result = a.strStr (haystack, needle); // Call strStr() and store the result
22+
23+ cout << result << endl; // Print the result (index of first occurrence or -1 if not found)
24+
25+ return 0 ; // Indicate successful program termination
26+ }
You can’t perform that action at this time.
0 commit comments