【问题标题】:How do I replace substrings in an array如何替换数组中的子字符串
【发布时间】:2019-04-18 20:45:57
【问题描述】:

我有一个程序可以将 char 数组的一部分替换为另一个数组的内容,但它也替换了不应替换的数组部分。

下面的代码是负责替换数组中字符的代码。 一次一个字符,但是当它应该替换的只有一部分在 xyxyz 之类的数组中时,它应该不理会它并寻找下一个出现的 xyz 然后它将替换。

i =0;
while(i < 30){
    j=0;
    while(j < k){
        if(carray[i] == aarray[j])
            carray[i] = barray[j];
        j++;
    }
    i++;
}

我的问题是,如果程序应该搜索和替换的内容只有一部分存在,它会替换它,即使它不是完整的单词。

例如:

如果我想在xyzdefxyzghixy 的数组中将xyz 替换为abc 那么数组有以下内容:

aarray[] = {xyz}
barray[] = {abc}
carray[] = {xyzdefxyzghixy}

我得到abcdefabcghiab作为输出,最后两个字符ab实际上应该保留xy


我希望输出是abcdefabcghixy

我在这里做错了什么。

注意:请注意,我希望只使用stdio.h

非常感谢所有帮助。

提前谢谢你。

【问题讨论】:

  • 内部循环不会检查是否所有的xyz 都可以匹配。它只是一个一个地测试每个字符。
  • 你只是在比较一个字符:你应该检查三个的整个序列。
  • 您的代码将替换x-&gt;a, y-&gt;b, z-&gt;c 任何出现这些字符的地方。
  • 使用 strstrmemcpy 简化工作,看我的回答
  • 感谢您的帮助,我想澄清一下,我希望只使用stdio.h

标签: c for-loop while-loop


【解决方案1】:

只有在找到完整的子字符串时才需要替换。

有一些有用的标准函数:

  • strstr 允许在字符串中查找子字符串
  • memcpy 可用于将旧子字符串替换为新子字符串

提案:

#include <stdio.h>
#include <string.h>

int main(int argc, char ** argv)
{
  if (argc != 4)
    printf("usage : %s <str> <old> <new>\n", *argv);
  else {
    char * old = argv[2];
    char * new = argv[3];
    size_t len = strlen(old);

    if (len != strlen(new))
      fprintf(stderr, "'%s' and '%s' do not have the same length\n", old, new);
    else {
      char * str = argv[1];
      char * p = str;

      printf("'%s' -> ", str);

      /* the loop doing the replacements */
      while ((p = strstr(p, old)) != NULL) {
        memcpy(p, new, len);
        p += len;
      }

      printf("'%s'\n", str);
    }
  }

  return 0;
}

编译和执行:

pi@raspberrypi:/tmp $ gcc -pedantic -Wextra -Wall r.c
pi@raspberrypi:/tmp $ ./a.out
usage : ./a.out <str> <old> <new>
pi@raspberrypi:/tmp $ ./a.out xyzdefxyzghixy xyz abc
'xyzdefxyzghixy' -> 'abcdefabcghixy'

编辑

注意:请注意,我希望只使用 stdio.h

可以:

#include <stdio.h>

int main(int argc, char ** argv)
{
  if (argc != 4)
    printf("usage : %s <str> <old> <new>\n", *argv);
  else {
    char * old = argv[2];
    char * new = argv[3];
    char * str = argv[1];

    /* check old and new have the same length */
    char * pold, * pnew;

    for (pold = old, pnew = new; *pold && *pnew; ++pold, ++pnew)
      ;

    if (*pold || *pnew)
      fprintf(stderr, "'%s' and '%s' do not have the same length\n", old, new);
    else {
      printf("'%s' -> ", str);

      char * pstr = str;

      /* the loop doing the replacements */
      while (*pstr) {
        /* check if substring */
        char * pold = old;
        char * psubstr = pstr;

        while (*pold && (*pold == *psubstr)) {
          pold += 1;
          psubstr += 1;
        }

        if (*pold == 0) {
          /* substring, replacement */
          pnew = new;

          while (*pnew)
            *pstr++ = *pnew++;
        }
        else
          pstr += 1;
      }

      printf("'%s'\n", str);
    }
  }

  return 0;
}

正如你所看到的,这是一个糟糕的选择,很容易理解以前版本的作用,而那个版本不是这样

【讨论】:

    猜你喜欢
    • 2021-06-23
    • 2015-12-25
    • 1970-01-01
    • 1970-01-01
    • 2019-03-16
    • 1970-01-01
    • 2021-09-15
    • 1970-01-01
    • 2014-06-09
    相关资源
    最近更新 更多