主要是体现思想,所以手动输入单词变成了写死在代码里面了。
然后其实也是对C指针的一次理解吧,这道题!
首先要在这个句子里面找我们单词的第一个字母,找到第一个字母之后,开始比较从这个字母往后的每一个字母,直到单词的最后一个字母为止,这些都做完了,还差一个工作,就是看句子中的该处该单词的最后一个字母的后一个字符是不是字母,如果是的话,证明这个单词只是一个长单词的一部分,所以不算该单词出现一次!(实例中的第三个grand不算,因为grandfather才算一个单词)
#include "stdio.h"
int main(){
char TargetStr[100] = "hey man,I am your grand grand grandfather!";
char Searchword[6] = "grand";
int count = 0;
int count1 = 0;
int index1,ind;
int status = 0;
for (index1 = 0; index1 < 100; index1++){
if (TargetStr[index1] == Searchword[0]){
ind = 1;
while (Searchword[ind] != '\0'){
if (TargetStr[index1 + ind] == Searchword[ind]){
ind++;
}
else{
goto here;
}
}
//Make sure there is no other letter after the last letter
if (!((TargetStr[index1 + 5] <= 90 && TargetStr[index1 + 5] >= 65) ||
(TargetStr[index1 + 5] >= 97 && TargetStr[index1 + 5] <= 122)))
{
count++;
}
here:;
}
}
printf("count = %d \n",count);
return 0;
}