moestr.cpp
//Searching String Using Knuth-Morris-Pratt Algorithm

Searching String Using Knuth-Morris-Pratt Algorithm
Searching String Using Knuth - Morris - Pratt Algorithm
int Str_KMP(char* str, char* sub)
{
	int c = Str_Len(sub);
	int i = 0, j = -1;
	int *s = (int*)malloc(c*sizeof(int));
	if (s == NULL)return(-1);
	s[0] = -1;
	c--;
	while (i < c)
	{
		if ((j == -1) || (sub[i] == sub[j]))
		{
			i++;
			j++;
			if (sub[i] != sub[j])
			{
				s[i] = j;
			}
			else
			{
				s[i] = s[j];
			}
		}
		else
		{
			j = s[j];
		}
	}
	c++;
	i = 0;
	j = 0;
	while (str[i] != '\0')
	{
		if (str[i] == sub[j])
		{
			i++;
			j++;
			if (j == c)
			{
				free(s);
				return(i - j);
			}
		}
		else
		{
			j = s[j];
			if (j == -1)
			{
				i++;
				j = 0;
			}
		}
	}
	free(s);
	return(-1);
}
int Str_KMP ( char * str , char * sub ) { int c = Str_Len ( sub ) ; int i = 0 , j = -1 ; int * s = ( int * ) malloc ( c * sizeof ( int ) ) ; if ( s == NULL ) return ( -1 ) ; s [ 0 ] = -1 ; c -- ; while ( i < c ) { if ( ( j == -1 ) || ( sub [ i ] == sub [ j ] ) ) { i ++ ; j ++ ; if ( sub [ i ] != sub [ j ] ) { s [ i ] = j ; } else { s [ i ] = s [ j ] ; } } else { j = s [ j ] ; } } c ++ ; i = 0 ; j = 0 ; while ( str [ i ] != ' \0 ' ) { if ( str [ i ] == sub [ j ] ) { i ++ ; j ++ ; if ( j == c ) { free ( s ) ; return ( i - j ) ; } } else { j = s [ j ] ; if ( j == -1 ) { i ++ ; j = 0 ; } } } free ( s ) ; return ( -1 ) ; }
Searching String Using Knuth-Morris-Pratt Algorithm
Searching String Using Knuth - Morris - Pratt Algorithm
//processing for the "next" array
 //finding the string
