我试图用单词“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);
}
我真的很感激任何提示!谢谢:)