C中KMP算法的有关问题,返回值总为0

C中KMP算法的问题,返回值总为0,求助
要利用KMP算法统计一个关键词在一篇文章中出现的次数,可是写完之后发现返回值总是0,半天都没搞好,很是困惑,希望各路好汉替我解决它~!

代码有些乱,还是初学者阶段,也请指教。

WIN7 64位 VC6.0编译


#include <stdio.h> 
#include <stdlib.h> 
#include <conio.h> 
#include <windows.h> 
#include <graphics.h> 
#include <string.h>

#define LEN 16
#define MAXLEN 10000
char str[MAXLEN];
int next[LEN];


void getnext(char *T, int next[])
{
int a=1,b=0;
next[1]=0;
while(a<T[0])
{
if(b==0||T[a]==T[b])
{
++a;
++b;
next[a]=b;
}
else
b=next[b];
}
}//getnext

int kmp(char *S, char *T, int pos)
{
int a=pos;
int b=1;
while(a<=S[0]&&b<=T[0])
{
if(b==0||S[a]==T[b])
{
++a;
++b;
}
else
b=next[b];
}
if(b>T[0])
return b-T[0];
else 
return 0;
}//KMP


void main()
{
   
char author[LEN];
char word1[LEN];
char word2[LEN];
char word3[LEN];
char file[1000];
int len;
int i=0;
  char ch;
int artnum=0;

printf("请输入作家姓名\n");
scanf("%s",author);
printf("请输入特征词1\n");
scanf("%s",word1);
printf("请输入特征词2\n");
scanf("%s",word2);
printf("请输入特征词3\n");
scanf("%s",word3);
printf("请输入文件位置,包括后缀名\n");
scanf("%s",file);


FILE *fp;
if((fp=fopen(file,"r"))==NULL)
{
printf("读取错误\n");
exit(1);
}

printf("\n");

while((ch=fgetc(fp))!=EOF)
{
str[i]=ch;
i++;

}
//str[i]='/0';
printf("%s\n",str);//将文件写入str串中
fclose(fp);

len=i;
printf("%d\n",len);//输出字符数

int j=0;
for(j=0;j<=len;j++)
{
if(str[j]==' ')
{
if(str[j+1]==' ')
continue;
if((str[j+1]>='A')&&(str[j+1]<='Z')||(str[j+1]>='a')&&(str[j+1]<='z'))
artnum++;
}
if(str[j]=='\n')
continue;
if(str[j]==',')
continue;
}//统计单词个数
printf("%d\n",artnum);//输出单词个数

getnext(word1,next);
int pos1;
pos1=kmp(str,word1,0);
printf("%d\n",pos1);
}

 


------解决方案--------------------
可以再想想KMP算法的思想,getnext()中T[0]的含义是什么,word1应该怎样输入等等。

看看这个:http://blog.csdn.net/v_july_v/article/details/7041827