Write the following function to check whether string s1 is a substring of string s2.


PHOTO EMBED

Sun Nov 15 2020 20:41:49 GMT+0000 (UTC)

Saved by @mahmoud hussein #c++

#include <iostream>
#include<cmath>
#include<ctime>
#include<string>
#include <iomanip>
#include <fstream>
// CPP program to check if a string is 
// substring of other. 

using namespace std;

// Returns true if s1 is substring of s2 
int isSubstring(string s1, string s2)
{
	int M = s1.length();
	int N = s2.length();

	/* A loop to slide pat[] one by one */
	for (int i = 0; i <= N - M; i++) {
		int j;

		/* For current index i, check for
pattern match */
		for (j = 0; j < M; j++)
			if (s2[i + j] != s1[j])
				break;

		if (j == M)
			return i;
	}

	return -1;
}

/* Driver program to test above function */
int main()
{
	string s1 ,s2 ;
	getline(cin, s1);
	getline(cin, s2);
	int res = isSubstring(s1, s2);
	if (res == -1)
		cout << "Not present";
	else
		cout << "Present at index " << res;
	return 0;
}
content_copyCOPY

http://cpp.sh/