File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -55,4 +55,5 @@ add_executable(cpp_evolution
5555 phase-3-data-structures/10_buy-sell.cpp
5656 phase-3-data-structures/11_anagrams.cpp
5757 phase-3-data-structures/12_reverse_string.cpp
58- phase-3-data-structures/15_remove_spaces.cpp )
58+ phase-3-data-structures/15_remove_spaces.cpp
59+ phase-3-data-structures/16_check_substrings_manually.cpp )
Original file line number Diff line number Diff line change 1+ // credits: https://www.geeksforgeeks.org/dsa/check-string-substring-another/
2+ // C++ program to check if a string is substring of other
3+ // using nested loops
4+
5+ #include < iostream>
6+ using namespace std ;
7+
8+ // Function to find if pat is a substring of txt
9+ int findSubstring (string &txt, string &pat) {
10+ int n = txt.length ();
11+ int m = pat.length ();
12+
13+ // Iterate through txt
14+ for (int i = 0 ; i <= n - m; i++) {
15+
16+ // Check for substring match
17+ int j;
18+ for (j = 0 ; j < m; j++) {
19+
20+ // Mismatch found
21+ if (txt[i + j] != pat[j]) {
22+ break ;
23+ }
24+ }
25+
26+ // If we completed the inner loop, we found a match
27+ if (j == m) {
28+
29+ // Return starting index
30+ return i;
31+ }
32+ }
33+
34+ // No match found
35+ return -1 ;
36+ }
37+
38+ int main () {
39+ string txt = " sayamapradhan" ;
40+ string pat = " a" ;
41+ cout << findSubstring (txt, pat);
42+
43+ return 0 ;
44+ }
You can’t perform that action at this time.
0 commit comments