我为concatenate two strings without using strcat编写了一个程序,为初学者使用两个while循环。需要你的建议用一个循环编码。请分享您的逻辑,谢谢
答案 0 :(得分:0)
char first[1000];
char second[2000];
char result[4000];
void concat(char *first, char *second, char *result){
int str_selector = 0;
int i = 0, result_pos = 0;
char *current_str = first;
while(1){
if(str_selector == 1 && current_str[i] == '\0'){
result[result_pos] = '\0';
break;
}
if(str_selector == 0)
current_str = first;
else
current_str = second;
if(current_str[i] != '\0'){
result[result_pos] = current_str[i];
result_pos++;
i++;
}
else{
i = 0;
str_selector = 1;
}
}
}