繁体   English   中英

查找并替换所有在C中出现的子字符串

[英]Find and replace all occurrences of a substring in C

我试图在C中的字符串数组中查找和替换子字符串的所有出现。我认为我的大部分逻辑都没有,但是我不知道其余部分的混乱之处。

这是相关的代码-我要替换的字符串在searchStr并且我试图用replaceStr替换它。 字符串数组称为buff 我不需要在将修改后的字符串保存回数组后,只需将修改后的字符串打印到控制台即可。

for (size_t i = 0; i < numLines; i++) {
        char *tmp = buff[i];
        char finalStr[MAX_STR_LEN * 2];
        char temporaryString[MAX_STR_LEN];
        int match = 0;
        while ((tmp = strstr(tmp, searchStr))) {
            match = 1;
            char temporaryString[MAX_STR_LEN];
            char tmp2[MAX_STR_LEN];
            printf("Buff[i]: %s", buff[i]);

            sprintf(temporaryString, "%s", strstr(tmp, searchStr) + strlen(searchStr)); // Grab everything after the match
            printf("Behind: %s", temporaryString);

            strncpy(tmp2, buff[i], tmp - buff[i]); // Grab everything before the match
            strcat(finalStr, tmp2);
            printf("In Front: %s\n", finalStr);

            strcat(finalStr, replaceStr); // Concat everything before with the replacing string

            tmp = tmp + strlen(searchStr);
            buff[i] = tmp; // Move buff pointer up so that it looks for another match in the remaining part of the string
        }
        if (match) {
            strcat(finalStr, temporaryString); // Add on any remaining bytes
            printf("Final: %s\n", finalStr); 
        }
    }

如果那里有很多printf ,那么我可以看到所有要调试的地方。

示例案例:

如果我使用searchStr = 4replaceStr = !!!对字符串what4is4this运行它replaceStr = !!! 这是控制台中的输出...我也使用//添加注释

Buff[i]: what4is4this           // Just printing out the current string before we attempt to replace anything
Behind: is4this                 // Looking good here
In Front: hat                   // Why is it cutting off the 'w'? 
Buff[i]: is4this                // Good - this is the remaining string we need to look through
Behind: this                    // Again, looking good
In Front: hat!!!isat            // It should be 'is'
Final: hat!!!isat!!!isat        // final should be 'what!!!is!!!this'

有想法吗? 我正在扯头发试图解决这个问题

谢谢!

这是指针摆弄和未定义行为的不健康组合,但是评论者已经告诉过您。 如果稍微简化一下,并善用节俭的指针,则可以执行以下操作:

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

// ALL CHECKS OMMITTED!

#define MAX_STR_LEN 1024

int main(int argc, char **argv)
{
  char *buff, *cpbuff;
  char *searchStr;
  char *replaceStr;

  // pointers too the two parts with the search string in between
  char *tmp, *after;
  // the final output (a fixed length is not good, 
  // should be dynamically allocated)
  char finalStr[MAX_STR_LEN * 2] = { '\0' };

  if (argc < 4) {
    fprintf(stderr, "Usage: %s string searchstr replacestr\n", argv[0]);
    exit(EXIT_FAILURE);
  }

  buff = malloc(strlen(argv[1]) + 1);
  strcpy(buff, argv[1]);
  searchStr = malloc(strlen(argv[2]) + 1);
  strcpy(searchStr, argv[2]);
  replaceStr = malloc(strlen(argv[3]) + 1);
  strcpy(replaceStr, argv[3]);

  // Keep a finger on the start of buff
  cpbuff = buff;
  while (1) {
    printf("Buff: %s\n", buff);
    // Grab everything after the match
    after = strstr(buff, searchStr);
    // No further matches? Than we're done
    if (after == NULL) {
      strcat(finalStr, buff);
      break;
    }
    // assuming strlen(searchStr) >= 1
    tmp = buff;
    // mark the end of the first part
    tmp[after - buff] = '\0';
    // set the after pointer to the start of the second part
    after = after + strlen(searchStr);
    printf("Behind: %s\n", after);
    printf("Before: %s\n\n", tmp);
    // Put the first part to it's final place
    strcat(finalStr, tmp);
    // concat the replacement string
    strcat(finalStr, replaceStr);
    // Set buff to the start of the second part
    buff = after + strlen(searchStr) - 1;
  }
  printf("Final: %s\n", finalStr);
  // set the buff pointer back to it's start
  buff = cpbuff;

  free(buff);
  free(searchStr);
  free(replaceStr);

  exit(EXIT_SUCCESS);
}

可以称为滥用指针算术的唯一点是标记第一部分结尾的线。 可以通过测量和使用相关字符串的长度并对其进行算术来避免这种情况。 承认,它比较慢,因此取决于您的个别用例。

它仍然比我喜欢的复杂,但这是一个开始。

我希望您现在可以自己将其扩展到多行输入。

暂无
暂无

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

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