建议使用一个循环来连接两个不带strcat的字符串的逻辑

Suggest logic with one loop to concatenate two strings without strcat

本文关键字:两个 strcat 字符串 一个 循环 连接      更新时间:2023-10-16

我为初学者编写了一个程序来连接两个字符串,不使用strcat,使用两个while循环。需要你的建议,编码它与一个循环。请分享你的逻辑,谢谢

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] == ''){
      result[result_pos] = '';
      break;
    }
    if(str_selector == 0)
      current_str = first;
    else
      current_str = second;
    if(current_str[i] != ''){
      result[result_pos] = current_str[i];
      result_pos++;
      i++;
    }
    else{
      i = 0;
      str_selector = 1;
    }
  }
}