我的任务是将v1
连接到v2
,功能my_strcat()
必须是void
。如何使用void函数返回连接的字符串?
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';
}
答案 0 :(得分:1)
void my_strcat (char s1[], char s2[],char result[]){
int result[16];
int i = 0;
int j = 0;
while(s1[i] != '\0'){
result[i]= s1[i];
++i;
}
while(s2[j] != '\0'){
result[i+j] = s2[j];
++j;
}
result[i+j] = '\0';
}
你可以这样做......我的main()声明第三个结果字符串,其大小是大小(v1)+大小(v2)+1
char result[33];
my_strcat(v1,v2,result);
输出:
Enter First Name: avinash
Enter Last Name: pandey
len: 7 - string: avinash
len: 6 - string: pandey
Concatenated String: avinashpandey