我的任务是将v1
连接到v2
,功能my_strcat()
必须是void
。我怎样才能使用void函数来返回串联的字符串?C中的连接字符串
int main(void){
char v1[16], v2[16];
int i1, i2;
printf("Enter First Name: \n");
scanf("%s", v1);
printf("Enter Last Name: \n");
scanf("%s", v2);
i1 = my_strlen(v1);
i2 = my_strlen(v2);
printf("len: %3d - string: %s \n", i1, v1);
printf("len: %3d - string: %s \n", i2, v2);
my_strcat(v1, v2);
printf("Concatenated String: %s \n", v1);
return 0;
}
void my_strcat (char s1[], char s2[]){
int result[16];
int i = 0;
int j = 0;
while(s1[i] != '\0'){
++i;
result[i]= s1[i];
}
while(s2[j] != '\0'){
++j;
result[i+j] = s2[j];
}
result[i+j] = '\0';
}
可能重复:http://stackoverflow.com/questions/308695/how-to-concatenate-const-literal-strings-in-c – sam 2015-04-02 18:41:41
只需通过一个缓冲器(例如数组)的函数存储新的字符串。 – teppic 2015-04-02 18:43:32
我该怎么做? @teppic – user4766244 2015-04-02 18:45:14