strcat() Function


The strcat() method joins two strings together. It takes the following form :

strcat(str1,str2);

str1 and str2 are character arrays. When the function strcat is executed, str2 is appended to str1. It does so by removing the null character at the end of str1 and placing str2 from there. The string at str2 remains unchanged. For example, consider the following three strings :

str1 = 0 1 2 3 4 5 6 7 8 9
V E R Y \0
str2 = 0 1 2 3 4 5 6
G O O D \0

strcat(str1,str2);


will result in :

str1 = 0 1 2 3 4 5 6 7 8 9
V E R Y G O O D \0

We must make sure that the size of str1 (to which str2 is appended) is large enough to accommodate the final string.

strcat() method may also append a string constant to a string variable. The following is valid :

strcat(str1,"GOOD");

c allows nesting of strcat() methods. For example, the statement

strcat(strcat(str1,str2),str3);

is allowed and concatenates all the three strings together. The resultant string is stored in str1.


Sample Program

Program
#include <stdio.h>
#include <string.h>

int main() {
  char str1[50] = "VERY ";
  char str2[20] = "GOOD ";
  char str3[20] = "BAD ";
  
  printf("%s",strcat(strcat(str1,str2),str3));
  return 0;
}