解释一下这个代码的意思i = 0; while (text[i] != '\0') { j = 0; found = 1; while (word[j] != '\0') { if (text[i+j] != word[j]) { found = 0; break; } j++; } if (found && (text[i+j] == ' ' || text[i+j] == '\n' || text[i+j] == '\0')) { printf("2020"); i += j; } else { printf("%c", text[i]); i++; }
时间: 2023-05-17 15:06:13 浏览: 55
= 1) { count++; } i++; } 这段代码是在统计一个字符串中某个单词出现的次数。首先,将计数器 i 初始化为 0,然后进入一个 while 循环,判断字符串 text 中的每个字符是否为字符串结束符 '\0'。在循环中,将计数器 j 初始化为 0,将 found 标志位设为 1。接着进入一个嵌套的 while 循环,判断字符串 word 中的每个字符是否与 text 中的对应字符相等。如果不相等,则将 found 标志位设为 0,并跳出循环。如果循环结束后 found 仍为 1,则说明找到了一个完整的单词,将计数器 count 加 1。最后将计数器 i 加 1,继续循环,直到找完整个字符串。
相关问题
void locating(char name[],char keys[]) { int i=1; //i为行号 int j=0; //j为列号(该行第几个单词) int k; //每一行出现的次数 char text; //存放进行比对的一行的字符串 int count=0; //出现总次数 //打开目标文件 FILE *fp; fp=fopen(name,"r"); if(fp==NULL) { printf("File Opening Failure"); exit(0); } keys[0]=length(keys); get_next(keys,next); printf("%s\n",&keys[1]); //打印关键字 while(!feof(fp)) { k=0; fgets(&text[1],MaxSize,fp); text[0]=length(text); j=KMP_match(text,keys,j+1); if(j!=0) { if(text[j+keys[0]]==' '||text[j+keys[0]]=='"'||text[j+keys[0]]==','text[j+keys[0]]=='!'||text[j+keys[0]]=='.'||text[j+keys[0]]=='?'||text[j+keys[0]]==':'||text[j+keys[0]]==';')//判断是否为完整单词 { printf("row=%d,column=%d",i,j); k++; } } while(j!=0) { j=KMP_match(text,keys,j+1); //调用KMP算法从刚找到的列号后一字符起匹配 if (j!=0) { if(text[j+keys[0]]==' '||text[j+keys[0]]=='"'||text[j+keys[0]]==','text[j+keys[0]]=='!'||text[j+keys[0]]=='.'||text[j+keys[0]]=='?'||text[j+keys[0]]==':'||text[j+keys[0]]==';')//判断是否为完整单词 { printf(",%d",j); //若匹配成功,则打印列号 k++; } } printf("\n"); i++; count=count+k; } fclose(fp); if(count) { printf("%s共出现%d次\n",&keys[1],count); } else { printf("Not Found the Keyword(s)\n"); } }
这段代码中存在一些问题:
1. 在判断完整单词时,多个判断条件之间缺少了 `||` 运算符,这会导致编译器报错。请将两个判断条件之间加上 `||` 运算符,如下所示:
```
if (text[j + keys[0]] == ' ' || text[j + keys[0]] == '"' || text[j + keys[0]] == ',' || text[j + keys[0]] == '!' || text[j + keys[0]] == '.' || text[j + keys[0]] == '?' || text[j + keys[0]] == ':' || text[j + keys[0]] == ';')
```
2. 在判断完整单词时,你的代码中判断字符的位置可能有问题。应该是 `text[j + keys[0]]` 而不是 `text[j - keys[0]]`。
3. 在判断完整单词时,你的代码中缺少了一个空格,这可能导致判断不准确。应该在逗号、感叹号、句号等字符之间加上空格,如下所示:
```
if (text[j + keys[0]] == ' ' || text[j + keys[0]] == '"' || text[j + keys[0]] == ',' || text[j + keys[0]] == '!' || text[j + keys[0]] == '.' || text[j + keys[0]] == '?' || text[j + keys[0]] == ':' || text[j + keys[0]] == ';')
```
4. 在打印列号时,第一次打印的列号前面应该没有逗号。
5. 在打印列号时,应该使用 `printf` 函数,而不是 `cout`。
经过修改后的代码如下所示:
```
void locating(char name[], char keys[]) {
int i = 1; // i为行号
int j = 0; // j为列号(该行第几个单词)
int k; // 每一行出现的次数
char text[MaxSize]; // 存放进行比对的一行的字符串
int count = 0; // 出现总次数
// 打开目标文件
FILE* fp;
fp = fopen(name, "r");
if (fp == NULL) {
printf("File Opening Failure");
exit(0);
}
keys[0] = length(keys);
get_next(keys, next);
printf("%s\n", &keys[1]); // 打印关键字
while (!feof(fp)) {
k = 0;
fgets(&text[1], MaxSize, fp);
text[0] = length(text);
j = KMP_match(text, keys, j + 1);
if (j != 0) {
if (text[j + keys[0]] == ' ' || text[j + keys[0]] == '"' || text[j + keys[0]] == ',' || text[j + keys[0]] == '!' || text[j + keys[0]] == '.' || text[j + keys[0]] == '?' || text[j + keys[0]] == ':' || text[j + keys[0]] == ';') // 判断是否为完整单词
{
printf("row=%d,column=%d", i, j); // 第一次打印的列号前面没有逗号
k++;
}
}
while (j != 0) {
j = KMP_match(text, keys, j + 1); // 调用KMP算法从刚找到的列号后一字符起匹配
if (j != 0) {
if (text[j + keys[0]] == ' ' || text[j + keys[0]] == '"' || text[j + keys[0]] == ',' || text[j + keys[0]] == '!' || text[j + keys[0]] == '.' || text[j + keys[0]] == '?' || text[j + keys[0]] == ':' || text[j + keys[0]] == ';') // 判断是否为完整单词
{
printf(",%d", j); // 使用 printf 函数打印列号
k++;
}
}
}
printf("\n");
i++;
count = count + k;
}
fclose(fp);
if (count) {
printf("%s共出现%d次\n", &keys[1], count);
}
else {
printf("Not Found the Keyword(s)\n");
}
}
```
void locating(char name[],char keys[]) { int i=1; //i为行号 int j=0; //j为列号(该行第几个单词) int k; //每一行出现的次数 char text; //存放进行比对的一行的字符串 int count=0; //出现总次数 //打开目标文件 FILE *fp; fp=fopen(name,"r"); if(fp==NULL) { printf("File Opening Failure"); exit(0); } keys[0]=length(keys); get_next(keys,next); printf("%s\n",&keys[1]); //打印关键字 while(!feof(fp)) { k=0; fgets(&text[1],MaxSize,fp); text[0]=length(text); j=KMP_match(text,keys,j+1); if(j!=0) { printf("row=%d,column=%d",i,j); k++; } while(j!=0) { j=KMP_match(text,keys,j+1); //调用KMP算法从刚找到的列号后一字符起匹配 if (j!=0) { printf(",%d",j); //若匹配成功,则打印列号 k++; } } printf("\n"); i++; count=count+k; } fclose(fp); if(count) { printf("%s共出现%d次\n",&keys[1],count); } else { printf("Not Found the Keyword(s)\n"); } }加入限制条件只有单词之后是空格才输出
你想要加入限制条件,只有单词之后是空格才输出,可以在KMP_match函数中加入判断条件。假设当前匹配到了文本串text的第j个位置,那么只有当text[j+keys[k]]是空格时,才说明关键字keys[k]匹配到了一个完整的单词,此时才输出列号。具体代码如下:
```
int KMP_match(char text[],char keys[],int start)
{
int i=start,j=1;
while(i<=text[0]&&j<=keys[0])
{
if(j==0||text[i]==keys[j]) //匹配成功,继续匹配下一个字符
{
i++;
j++;
}
else //匹配失败,回溯到前面位置重新开始匹配
{
j=next[j];
}
if(j>keys[0]) //匹配成功,返回列号
{
if(text[i+1]==' '||text[i+1]=='\n') //判断是否为完整单词
return i-keys[0]+1;
else
j=1;
}
}
return 0; //匹配失败,返回0
}
```
在locating函数中,将输出列号的部分修改为:
```
if(j!=0)
{
if(text[j+keys[0]]==' '||text[j+keys[0]]=='\n') //判断是否为完整单词
{
printf("row=%d,column=%d",i,j);
k++;
}
}
```
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)