首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >查找并替换C中出现的所有子字符串

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

Stack Overflow用户
提问于 2016-09-08 22:23:51
回答 1查看 1.2K关注 0票数 0

我试图在C中的字符串数组中找到并替换所有出现的子字符串,我想我已经有了大部分的逻辑,但是我不知道我在哪里搞砸了剩下的部分。

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

代码语言:javascript
复制
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在那里,那么我就可以看到所有需要调试的地方。

示例案例:

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

代码语言:javascript
复制
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!       // final should be 'what!is!!!this'

伙计们有什么想法吗?我把头发扯掉,试图解决这个问题

谢谢!

EN

回答 1

Stack Overflow用户

发布于 2016-09-08 23:39:51

这是一个不健康的组合指针杂耍和未定义的行为,但评论者已经告诉你。如果你把它简化一点,好好地、节俭地使用指针,你就可以做以下事情:

代码语言:javascript
复制
#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);
}

可以称为滥用指针算法的单个点是标记第一部分结尾的行。通过测量和使用所涉及的字符串的长度并与它们一起进行算术,可以避免这一问题。它是比较慢的,承认,所以它取决于您的个别用例。

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

我希望您现在可以把它扩展到几个输入行。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/39401070

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档