这是相关的代码。当我尝试dest,src,start和replace的一些值时,在这些行上出现错误:
strncat(&returnV[i], &dest[i], 1);
strncat(&returnV[i], &src[i - start], 1);
strncat(&returnV[i], &src[i - start], 1);
strncat(&returnV[i], &dest[i - strlen(src) + replace], 1);
下面是相关代码的其余部分。
char * insertString(char * dest, char * src, int start, int replace) {
static char *returnV = "";
for(unsigned int i = 0; i < start; i++) {
strncat(&returnV[i], &dest[i], 1);
}
for(unsigned int i = start; i < start + replace; i++) {
strncat(&returnV[i], &src[i - start], 1);
}
for(unsigned int i = start + replace; i < (start + strlen(src)); i++) {
strncat(&returnV[i], &src[i - start], 1);
}
for(unsigned long i = start + strlen(src); i < (strlen(src) + strlen(dest)); i++) {
strncat(&returnV[i], &dest[i - strlen(src) + replace], 1);
}
return returnV;
}
我认为这是发生错误的地方,我试图将其修复为无济于事。如果需要的话,这是其余的代码:
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
char * insertString(char * dest, char * src, int start, int replace);
int main(int argc, const char * argv[]) {
char * result;
char dest[50];
scanf("%s", dest);
char src[50];
scanf("%s", src);
int start;
scanf("%d", &start);
int replace;
scanf("%d", &replace);
result = insertString(dest, src, start, replace);
printf("%s", result);
return 0;
}
char * insertString(char * dest, char * src, int start, int replace) {
static char *returnV = "";
for(unsigned int i = 0; i < start; i++) {
strncat(&returnV[i], &dest[i], 1);
}
for(unsigned int i = start; i < start + replace; i++) {
strncat(&returnV[i], &src[i - start], 1);
}
for(unsigned int i = start + replace; i < (start + strlen(src)); i++) {
strncat(&returnV[i], &src[i - start], 1);
}
for(unsigned long i = start + strlen(src); i < (strlen(src) + strlen(dest)); i++) {
strncat(&returnV[i], &dest[i - strlen(src) + replace], 1);
}
return returnV;
}
非常感谢您的帮助!