我的任務是將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