Modify a char* string in C

前端 未结 6 1316
既然无缘
既然无缘 2021-01-27 23:32

I have this:

char* original = \"html content\";

And want to insert a new

char* mycontent = \"newhtmlinsert\";

6条回答
  •  Happy的楠姐
    2021-01-28 00:03

    Assuming that char* original is composed by two parts, one starts at 0 while the other (html content after) starts at x you can use strcat and memcpy:

    int length = strlen(original)+strlen(newcontent)+1;
    char *neworiginal = malloc(sizeof(char)*length);
    memset(neworiginal, 0, length);
    memcpy(neworiginal,original,x*sizeof(char));
    strcat(neworiginal,newcontent);
    strcat(neworiginal,original+x);
    

提交回复
热议问题