替换C语言中的单词

4
我正在尝试创建一个函数来替换单词。它还支持用户输入的多个参数(单词)。例如,我们有一个名为“fesf”的文本文件,其中包含以下内容:“这是一个测试。”当我们键入
./a.out test is < fesf.txt
它应该在fesf.txt中用“censor”替换“test”和“is”。 (请注意,新行、排列应与原始文件相同,只有单词被替换)。我创建了这个函数,但不知何故它不起作用。返回时,它不会替换参数,只会打印原始文本。
以下是该函数:
   #include <stdio.h>
   #include <string.h>

   int main (int argc, char* argv[])

    {

     int i, j,c, state, wordlen, counter, found;
     char word[128];

     state = 0;
     wordlen = 0;

     while (( c= getchar())!= EOF) {

     if ((c >= 'A' && c <= 'Z') || (c == 39))

      {
        state =1;
        word[wordlen] = c;
        wordlen++;

      }

       else if (state == 1)

      {
        state =0;
        found =0;


            }

1
或者,你知道 isalpha(c) || c == 39。但是 ´ 有什么特别之处呢? - EOF
@EOF:OP想要“isn't”,“can't”,“it's”等被视为单个单词。 - Jongware
第一条评论解决了我的问题。 - Andrew
1个回答

2

第一个 if 需要捕获大小写字母,一种做法是:

if ((c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z') || (c == 39))

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接