Knuth–Morris–Pratt Algorithm

Knuth–Morris–Pratt (a.k.a KMP Algorithm) is a string search algorithm. it searches for occurrences of a sub-string within a main-string by employing the observation that when a mismatch occurs, the word itself embodies sufficient information to determine where the next match could begin, thus bypassing re-examination of previously matched characters.



									int* AddItemInArray(int** arr, int count, int item) {
	int* newArr = malloc(sizeof(int) * (count + 1));

	for (int i = 0; i < count; i++) {
		newArr[i] = arr[i];
	}

	newArr[count] = item;

	return newArr;
}

void ComputeLPSArray(char* pat, int m, int* lps)
{
	int len = 0;
	int i = 1;

	lps[0] = 0;

	while (i < m)
	{
		if (pat[i] == pat[len])
		{
			len++;
			lps[i] = len;
			i++;
		}
		else
		{
			if (len != 0)
			{
				len = lps[len - 1];
			}
			else
			{
				lps[i] = 0;
				i++;
			}
		}
	}
}

int* SearchString(char* str, char* pat) {
	int* retVal = malloc(sizeof(int));
	int retValSize = 0;
	int M = strlen(pat);
	int N = strlen(str);
	int i = 0;
	int j = 0;
	int* lps = malloc(sizeof(int) * M);

	ComputeLPSArray(pat, M, lps);

	while (i < N)
	{
		if (pat[j] == str[i])
		{
			j++;
			i++;
		}

		if (j == M)
		{
			retVal = AddItemInArray(retVal, retValSize++, i - j);
			j = lps[j - 1];
		}

		else if (i < N && pat[j] != str[i])
		{
			if (j != 0)
				j = lps[j - 1];
			else
				i = i + 1;
		}
	}

	free(lps);

	return retVal;
}
								


Example

									char* data = "the quick brown fox jumps over the lazy dog";
int* value = SearchString(data, "the");
								


Output

									0
31