C Tutorial/String/String Join

Материал из C\C++ эксперт
Перейти к: навигация, поиск

Joining strings without using strcat

<source lang="cpp">#include <stdio.h> int main(void) {

 char str1[40] = "AAA";
 char str2[] = "BBBB";
 int str1Length = 0;             
 int str2Length = 0;             
 while (str1[str1Length])          
   str1Length++;                   
 while (str2[str2Length])          
   str2Length++;
 if(sizeof str1 < str1Length + str2Length + 1)
   printf("\n str1 is too short.");
 else
 {  
   str2Length = 0;                 
   while(str2[str2Length]){   
     str1[str1Length++] = str2[str2Length++];
   }
   str1[str1Length] = "\0";        
   printf("\n%s\n", str1 );     
 }
 return 0;

}</source>

AAABBBB