c语言如何实现在给定文档中查找想要的单词或句子,就好像word中一样,50分求解!

2025-03-09 20:05:58
推荐回答(5个)
回答1:

//  这里提供一个在字符串中查找字串的函数findstr(),并由运行截图,供参考。

#include 

int findstr(char *s,char *d) {

char *p = s,*q;

int i;

while(*p) {

i = p - s;

q = d;

while(*p == *q) { p++; q++; }

if(*q == '\0') return i;

p = s + (++i);

}

return -1;

}

int main() {

char *s = "精品办公水杯";

char *d = "杯";

int result = findstr(s,d);

if(result >= 0) printf("【%s】在【%s】中的索引位置是:%d。\n\n",d,s,result);

else printf("没找到。\n\n");

return 0;

}

回答2:

给你说第一问吧,第一问知道了,第二问自然也就知道了。

首先在当前目录建一个文件用来测试程序,我建的是info.txt,内容是:
boss boy 23 100
susan girl 20 99

#include
#include
#include

int main(void)
{
FILE *fp;
struct info
{
char name[10];
char sex[5];
int num;
float score;
} stu;

// 打开文件info.txt来读
fp = fopen("./info.txt", "r");
if (NULL == fp)
return -1;

// 从开头到文件尾部,一行一行地读
while (EOF != fscanf(fp, "%s%s%d%f",
stu.name, stu.sex, &stu.num, &stu.score))
{
// 如果某行符合条件就输出。
if (0 == strcmp(stu.name, "susan"))
{
printf("%s %s %d %f\n", stu.name, stu.sex, stu.num, stu.score);
}
}

fclose(fp);
return 0;
}

回答3:

你也就只是一个在文件中实现查找一个字符或是一个字符串,以某特定格式出来。
我曾帮别人回答过一个查找字符串的问题,你作参考,另外,还要在查找之前要实现文件操作(文件的打开和关闭)
http://zhidao.baidu.com/question/238578960.html

在本列中你想方设法,把指针变量s指向你要打的文件。

回答4:

ALT+E , F

回答5:

三国