C - Append two string together

Introduction

We use strcat() function to add two string value together.

Here's how it works:

strcat(first,second); 

After this statement executes, the text from the second string is appended to the first string. Or you can use immediate values:

strcat(str,"ing"); 

The following code declares two char arrays to hold text.

Array first is twice as large as array last.

The copying takes place with the strcat() function.

Demo

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

int main() // w w  w.j  a v  a2 s  .  c o m
{ 
   char first[40]; 
   char last[20]; 

   printf("What is your first name? "); 
   scanf("%s",first); 
   printf("What is your last name? "); 
   scanf("%s",last); 
   strcat(first,last); 
   printf("Pleased to meet you, %s!\n",first); 
   return(0); 
}

Result

Related Topic