将字符串(一个字符一个字符)插入一个较大的字符串 [英] Inserting string (character by character) into a larger string

查看:151
本文介绍了将字符串(一个字符一个字符)插入一个较大的字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试通过用新字符串(toInsert)替换(原始)字符串中的子字符串来插入字符串。 start参数是我要替换的子字符串的开始位置。注意:这是较大程序的一部分,但我只是想使此功能正常工作。

I am trying to insert a string by replacing a substring in the (original) string with a new string (toInsert). The start parameter is the starting position of the substring I want to replace. Note: This is part of a larger program but I am just trying to get this function to work.

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define ROW 5
#define COLUMN 81

void insertString(char original[], int start, int length, char toInsert[]){

char buffer[COLUMN];
int i = 0;

for (i=0; i<strlen(original); ++i){
    if (i>=start){
        buffer[i] = toInsert[i];
    }
    else{
        buffer[i] = original[i];
    }
}

buffer[strlen(buffer)-1] = '\0';



printf("%s\n", buffer);



return;
}

int main()
{
char rep[COLUMN] = "very";
char sub[COLUMN] = "are";

char buf[ROW][COLUMN] = {
    {"How are you doing"}


};
int len = strlen(sub);
int start = 4;

insertString(buf[0], start, len, rep);



return 0;
}

问题在于它仅打印 How。

The problem is it only prints "How".

我希望它打印您的状况如何

I want it to print "How very you doing"

我也尝试过使用strcpy,但这只会给出错误/警告(与指针有关,并且我不想处理指针,因为我还没有了解它们。)

Also, I have tried using strcpy but that just gives errors/warnings (something to do with pointers and I don't want to deal with pointers because I have not learned about them yet.)

推荐答案

您的函数没有太大意义,因为它既不必扩大或缩小原始字符串,尽管它必须这样做。

Your function does not make great sense because it neither enlarge or shrink the original string though it has to do this.

此外,由于此if语句,它具有未定义的行为,因为当 i 等于或大于 start 时,您可以访问字符串 toInsert以外的内存使用索引 i

And moreover it has undefined behavior due to this if statement because when i is equal to or greater than start you can access the memory beyond the string toInsert using the index i.

if (i>=start){
    buffer[i] = toInsert[i];
}

使用标准C函数编写该函数更为简单。

It is simpler to write the function using standard C functions.

您在这里

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

char * replaceString( char *s1, size_t pos, size_t n, const char *s2 )
{
    size_t n1 = strlen( s1 );

    if ( pos < n1 )
    {
        size_t n2 = strlen( s2 );

        if ( n != n2 )
        {

            memmove( s1 + pos + n2, s1 + pos + n, n1 - pos - n + 1 );
        }

        memcpy( s1 + pos, s2, n2 );
    }

    return s1;
}


int main(void) 
{
    {
        char s1[100] = "ab1111cd";
        const char *s2 = "22";

        puts( replaceString( s1, 2, 4 , s2 ) );
    }

    {
        char s1[100] = "ab11cd";
        const char *s2 = "2222";

        puts( replaceString( s1, 2, 2 , s2 ) );
    }

    {
        char s1[100] = "ab11cd";
        const char *s2 = "22";

        puts( replaceString( s1, 2, 2 , s2 ) );
    }

    return 0;
}

程序输出为

ab22cd
ab2222cd
ab22cd

如果要插入此代码块

{
    char s1[100] = "How are you doing";
    const char *s2 = "very";

    puts( replaceString( s1, 4, 3 , s2 ) );
}

在演示程序中,您将获得输出

in the demonstrative program you will get the output

How very you doing

这篇关于将字符串(一个字符一个字符)插入一个较大的字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆