C String Append

Syntax

C strcat function has the following syntax.

char *strcat(char *str1, const char *str2);

Example 1


#include <string.h>
#include <stdio.h>
int main()//w  w  w  . jav a2s  .  c  o m
{
    char first[100];
    char last[100];
    char full_name[200];


    strcpy(first, "first");
    strcpy(last, "last");

    strcpy(full_name, first);


    strcat(full_name, " ");
    strcat(full_name, last);

    printf("The full name is %s\n", full_name);

    return (0);
}

The code above generates the following result.

Example 2


#include <stdio.h>
/*from   w  w  w.  j a v a2s.  c o m*/
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;
}

The code above generates the following result.





















Home »
  C Language »
    Language Advanced »




File
Function definition
String
Pointer
Structure
Preprocessing