简体   繁体   English

如何替换数组中的子字符串

[英]How do I replace substrings in an array

I have a program which replaces a parts of a char array with the contents of another, however it also replaces parts of the array that should not be replaced.我有一个程序,它用另一个字符数组的内容替换一个字符数组的一部分,但是它也替换了不应该替换的数组部分。

The code below is the code responsible for the replacing of characters in the array.下面的代码是负责替换数组中字符的代码。 One character at a time, however when only part of what it is supposed to replace is in an array like xy of xyz it should leave that alone and look for the next occurrence of xyz which it would then replace.一次一个字符,但是当它应该替换的内容的一部分在像xyxyz这样的数组中时,它应该xyz它并寻找下一次出现的xyz然后它会替换。

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

My problem is, that if only a part of what the program is supposed to search and replace for is present it replaces it even though it is not the full word that was meant.我的问题是,如果程序应该搜索和替换的内容的一部分存在,它会替换它,即使它不是完整的词。

for example:例如:

if I want to replace xyz with abc in the array of xyzdefxyzghixy then the arrays have the following content:如果我想用xyzdefxyzghixy数组中的abc替换xyz则数组具有以下内容:

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

I get abcdefabcghiab as an output and the last two characters ab should actually remain xy我得到abcdefabcghiab作为输出,最后两个字符ab实际上应该保持xy


ie IE

I expect the output to be abcdefabcghixy我希望输出是abcdefabcghixy

what have I done wrong here.我在这里做错了什么。

NB: please Note that I wish to only use stdio.h注意:请注意,我只想使用stdio.h

All help is greatly appreciated.非常感谢所有帮助。

Thank you in advance.先感谢您。

You need to replace only when you find the full substring.只有在找到完整的子字符串时才需要替换。

There are useful standard functions :有一些有用的标准函数:

  • strstr allows to find a substring in a string strstr允许在字符串中查找子字符串
  • memcpy can be used to replace the old substring by the new one memcpy可用于用新的子串替换旧的子串

A proposal :一份提案 :

#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;
}

Compilation and execution :编译和执行:

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'

Edit编辑

NB: please Note that I wish to only use stdio.h注意:请注意,我只想使用 stdio.h

Can be :可 :

#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;
}

As you can see this is a bad choice, it is very easy to understand what the previous version does, this is not the case with that version正如您所看到的,这是一个糟糕的选择,很容易理解以前版本的作用,而那个版本则不然

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM