简体   繁体   English

使用指向字符串的指针数组将字符串中的子字符串替换为c中的另一个子字符串

[英]Replacing a substring in a string with another substring in c using array of pointers to string

I am trying to replace a substring inside a string with another substring. 我试图用另一个子字符串替换字符串中的子字符串。 However, I am getting a segmentation fault at line strcpy(p, str2); 但是,我在strcpy(p,str2)行遇到了分段错误。 I do not understand why. 我不理解为什么。 Here is my code 这是我的代码

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


int main()
{
    char *str[] = {"We will teach you how to",
        "Move a mountain",
        "Level a building",
        "Erase the past",
        "Make a million",
        "...all through C"
    };
    char str1[20], str2[20];
    printf("Enter first string\n");
    scanf("%s", str1);
    printf("Enter second string\n");
    scanf("%s", str2);
    char *news, *t, *p;
    int i;
    if(strlen(str2) > strlen(str1))
    {
        printf("Second string should be smaller than first string\n");
        exit(1);
    }
    for(i = 0; i < 6; i++)
    {
        p = strstr(str[i], str1);
        if(p)
        {
            news = p + strlen(str1);
            strcpy(t,news);
            strcpy(p, s);
            strcat(p,t);
            break;
        }
    }
    printf("the new string is\n");
    for( i = 0; i < 6; i++)
    {
        printf("%s\n", str[i]);
    }
    return 0;
}

You're copying to the string t , but you've never initialized it to point anywhere. 您将复制到字符串t ,但从未将其初始化为指向任何地方。

strcpy(t, news);

This is bad news — oh, no, it's bad t and good news. 这是个坏消息-哦,不,这是不好的t的喜讯。

You have a second problem, too. 您也有第二个问题。 You're trying to modify the strings in the str array of pointers, but those are string literals. 您正在尝试修改指针的str数组中的字符串,但这是字符串文字。 You can't modify string literals reliably. 您不能可靠地修改字符串文字。 You'll have to modify things so that you don't do that. 您必须进行修改,以免您这样做。

p = strstr(str[i], str1);  // Generates a pointer into str[i]
...
strcpy(p, s);    // Modifies str[i] -- unsuccessfully

Probably the easiest (but not necessarily best) fix for this is to use: 为此,最简单(但不一定最好)的解决方法是使用:

char str[][64] = {"We will teach you how to",
    "Move a mountain",
    "Level a building",
    "Erase the past",
    "Make a million",
    "...all through C"
};

I've not spent a long time cogitating on the correct size for the second dimension of the array, but 64 looks like it probably leaves enough space for you to grow the strings a bit. 我没有花很长时间来思考数组第二维的正确大小,但是64看起来可能为您留出了足够的空间来稍微增加字符串。 Maybe that doesn't matter since there's a comment about the second string must be shorter than the first, so you're only dealing with shrinking strings. 也许没关系,因为关于第二个字符串的注释必须比第一个短,因此您只需要处理缩小的字符串。 Lucky you to be able to do that. 幸运的是您能够做到这一点。 You'd have to be careful to avoid troubles if your strings can grow. 如果您的琴弦可能长大,则必须小心避免麻烦。

As first noted by t0mm13b in a comment , there's no variable s in the code. 正如t0mm13b评论中首先指出的那样 ,代码中没有变量s Please make sure you post compilable code. 请确保您发布了可编译的代码。

Looking further at that inner loop, we can see all sorts of problems: 进一步看一下内部循环,我们可以看到各种问题:

  1. The news = p + strlen(str1); news = p + strlen(str1); assignment means that news is pointing to tail of str[i] after the str1 was found in str[i] . 赋值意味着在str[i]找到str1之后, news指向了str[i]尾部。
  2. You copy an indeterminate amount of information from news into the space pointed at by an uninitialized pointer, t . 您将news不确定的信息量复制到未初始化的指针t指向的空间中。 This is a first source of core dump. 这是核心转储的第一个来源。
  3. You copy the undefined variable s over p . 您将未定义的变量s复制到p Is that meant to be str2 , since str2 is not used after the length check? 那是不是str2 ,因为在长度检查之后不使用str2 This is a second source of core dump. 这是核心转储的第二个来源。
  4. You copy the material from t after the material copied from s . s复制材料之后,您可以从t复制材料。 If you've not already dumped core, this could be a third source of core dump (though maybe if you've not dumped core yet, this won't trigger one either). 如果您还没有转储核心,那么这可能是核心转储的第三个来源(尽管如果您尚未转储核心,这也不会触发任何一个)。

You probably need: 您可能需要:

char *p = strstr(str[i], str1);
if (p != 0)
{
    char t[64];
    strcpy(t, p + strlen(str1));
    strcpy(p, str2);
    strcat(p, t);
}

This is far from being proof from overflows, but is more nearly sound. 这远不能证明没有溢出,但是更接近声音。

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

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