汇编,查找匹配的字符串

来源:百度知道 编辑:UC知道 时间:2024/05/19 17:40:04
汇编,查找匹配的字符串
.实验内容:程序接收用户键入的一个关键字及一个句子。如果句子中不包含关键字则红色字显示'No match';如果句子中包含关键字则显示'match'.并用白色字显示句子,关键字用红色显示..
最好后面加上解释!!谢谢了

//KMP(D.E.Knuth---J.H.Morris---V.R.Pratt)算法,用于从一个字符串中查找子串

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

//求失效函数
int *FailFunction(short *str)
{
int i, j, len;
int *f;

len = strlen((char *)str)/2;
f = (int *)malloc(len*sizeof(int));
if(f == NULL)
{
printf("Allocate memory fail!\n");
exit(1);
}

f[0] = -1;
for(j=1; j<len; j++)
{
i = f[j-1];
while((*(str+j) != *(str+i+1)) && (i >= 0))
i = f[i];
if(*(str+j) == *(str+i+1))
f[j] = i+1;
else
f[j] = -1;
}

return f;
}

//从源串pSour中查找字符串pFind
char *FindStr(short