【发布时间】:2016-07-23 10:56:15
【问题描述】:
我正在尝试用“CENSORED”这个词替换传入的词,但我不知道在哪里解释替换词和被删节之间的区别。这是输入和输出的示例。
./a.out Ophelia draw or <poem.txt
Said Hamlet to CENSORED,
I'll CENSOREDsketch of thee,
What kind of pencil shall I use?
2B CENSORED2B?
但正确的输出应该是:
Said Hamlet to CENSORED,
I'll CENSORED a sketch of thee,
What kind of pencil shall I use?
2B CENSORED not 2B?
完整代码:
#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(int argc, char **argv){
char fileRead[4096];
char replace[] = "CENSORED";
int arg=0;
size_t word_len = strlen(argv[arg]);
while (fgets(fileRead, sizeof(fileRead), stdin) != 0)
{
char *start = fileRead;
char *word_at;
for (arg = 1; arg < argc; arg += 1) {
if ((word_at = strstr(start, argv[arg])) != 0) {
printf("%.*s%s", (int)(word_at - start), start, replace);
start = word_at + word_len -1;
}
}
printf("%s", start);
}
printf("\n");
return (0);
}
我非常感谢任何提示!谢谢:)
【问题讨论】:
标签: c string algorithm file-io file-read