C Tutorial/String/String Join


Deprecated: Constant E_STRICT is deprecated in /var/www/cppe.ru/www/includes/exception/MWExceptionHandler.php on line 179

Deprecated: Creation of dynamic property PPDStack::$accum is deprecated in /var/www/cppe.ru/www/includes/parser/Preprocessor_DOM.php on line 803

Deprecated: Constant E_STRICT is deprecated in /var/www/cppe.ru/www/includes/exception/MWExceptionHandler.php on line 179

Deprecated: Constant E_STRICT is deprecated in /var/www/cppe.ru/www/vendor/mediawiki/at-ease/src/Functions.php on line 48
Материал из C\C++ эксперт
Перейти к: навигация, поиск

Joining strings without using strcat

#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;
}
AAABBBB