关于关键词检测
这是一道作业题:有5个预先设定的关键词。编程输入一行字符串,从前到后找出其中的关键词及次数
程序代码://预设关键词:a the it that there
#include <stdio.h>
#include <stdlib.h>
void Detection(char sentence[],char keyWords[],int x);//关键词检测函数
int main()
{
printf("\n");
char sentence[500];
gets(sentence);
char keyWords[][81]={" a "," the "," it "," that "," there "};
Detection(sentence,keyWords[0],3);
Detection(sentence,keyWords[1],5);
Detection(sentence,keyWords[2],4);
Detection(sentence,keyWords[3],6);
Detection(sentence,keyWords[4],7);
return 0;
}
void Detection(char sentence[],char keyWords[],int x)//x是关键词的字幕个数,包括词前后的空格
{
int i,j,n,count=0;
for(i=0;sentence[i]!="\0";i++)//这里控制sentence
{
for(j=0,n=0;keyWords[j]!="\0"&&sentence[i+j]!="\0";j++)//这里控制keyWords
{
if(sentence[j+i]==keyWords[j])
n++;//此处n计数相同字母连续出现的次数
}
if(n==x)//若n和传入的关键词字母个数相同
count++;
}
printf("%s,%d",keyWords,count);
}
//死循环
这个程序跑起来输入句子之后崩溃,单步调试时在第二个for循环处进入死循环,好象是无法检测到循环判断条件中预设的keyWords[j]!="\0",然后j无限增长进入死循环,难道是编译器不认\0吗???求大神解答






