C String Functions - C strcat






Concatenates two strings.

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

Appends the string str2 to the end of the string str1.

The terminating null character of str1 is overwritten.

Copying stops once the terminating null character of str2 is copied.

If overlapping occurs, the result is undefined.

The argument str1 is returned.

Prototype

char * strcat ( char * destination, const char * source );

Parameter

This function has the following parameter.

destination
Pointer to the destination array.
source
C string to be appended.

Return

destination is returned.

Example


#include <stdio.h>
#include <string.h>
/*  ww  w .  ja  va  2 s.c  o  m*/
int main (){
  char str[80];
  strcpy (str,"these ");
  strcat (str,"strings ");
  strcat (str,"are ");
  strcat (str,"concatenated.");
  puts (str);
  return 0;
} 

       

The code above generates the following result.





Example 2


#include <string.h> 
#include <stdio.h>
#include <stdlib.h>
 /*  w w  w.  j a  v  a 2  s .  c  o  m*/
int main(void) {
    char str[50] = "Hello ";
    char str2[50] = "World!";
    strcat(str, str2);
    strcat(str, " ...");
    strcat(str, " Goodbye World!");
    puts(str);

}

The code above generates the following result.