top of page
Writer's picture_Romeyo Boy_

C String Concatenation : strcat( )

Updated: Feb 7, 2022


The strcat(first_str, second_str) function concatenates the two strings and the result is returned to first_str.

सी स्ट्रिंग कॉनकैटिनेशन : strcat( ) - क्रिएटिव ब्लोक

In the C programming language, the strcat( ) function appends a copy of the string pointed to by second_str2 to the end of the string pointed to by first_str1. It returns a pointer to srt1 where the resulting concatenated string resides.


* Syntax:

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


* Parameters or Arguments :

  • str1 - A pointer to a string that will be modified. str2 will be copied to the end of str1.

  • str2 - A pointer to a string that will be appended to the end of str1.

  • src - This is the string to be appended. It should not overlap the destination.

* Returns:

strcat फ़ंक्शन str1 पर एक पॉइंटर लौटाता है ( जहां परिणामी संयोजित स्ट्रिंग रहती है )।


* Required Header :

In the C language, the required header for the strcat( ) function is:

#include <string.h>


* Pay Attention :

Use the strcat() function with care, because it's easy to add more bytes to your variable using the strcat() function, which can lead to unexpected behavior.


* It Applies :

In the C language, the strcat() function can be used in the following versions:

  • ANSI/ISO 9899-1990


< > Code Example: 01.

/* Example using strcat by Creative Bloke */

#include <stdio.h>  
#include <string.h>   
 
int main()
{    
    char ch[10] = {'H', 'e', 'l', 'l', 'o', '\0'};    
    char ch2[10] = {'C', '\0'};   
 
    strcat(ch, ch2);    
    printf("Value of first string is: %s", ch);    

    return 0;    
}    

The output of this program is ;

Value of first string is: HelloC


< > Code Example : 02.

/* Example using strcat by Creative Bloke */

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

int main(int argc, const char * argv[])
{
    // Define a temporary variable
    char demo[100];

    // Copy the first string into the variable
    strcpy(demo, "creativebloke.in");

    // Concatenate the following two strings to the end of the first one
    strcat(demo, " is over 2");
    strcat(demo, " years old.");

    // Display the concatenated strings
    printf("%s \n", demo);

    return 0;
}

The output of this program is;

creativebloke.in is over 2 years old.


< > Code Example: 03.

/* Example using strcat by Creative Bloke */

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

int main () 
{
   char src[50], dest[50];

   strcpy(src,  "This is source");
   strcpy(dest, "This is destination ");

   strcat(dest, src);

   printf("Final destination string : %s", dest);
   
   return 0;
}

The output of this program is;

Final destination string : This is destination This is source



< > Code Example: 04.

/* Example using strcat by Creative Bloke */

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

int main() 
{
    char str1[50], str2[50];
    
    // destination string
    strcpy(str1, "This is my initial string");
    
    // source string
    strcpy(str2, ", add this");
   
    // concatenating the string str2 to the string str1
    strcat(str1, str2);
   
    //displaying destination string
    printf("String after concatenation: %s", str1);
    
    return(0);
}

The output of this program is;

String after concatenation: This is my initial string, add this

< > Code Example: 05.

/* Example using strcat by Creative Bloke */

#include <stdio.h>
#include <string.h>
 
int main( )
{
    char source[ ] = "Creative Bloke" ;
    char target[ ]= "C Tutorial " ;
 
    printf( "\n Source string = %s", source ) ;
    printf( "\n Target string = %s", target ) ;
 
    strcat( target, source ) ;
 
    printf("\n Target string after strcat( ) = %s", target ) ;
}

The output of this program is;

Source string = Creative Bloke
Target string = C Tutorial 
Target string after strcat( ) = C Tutorial Creative Bloke

✨ Also know some related articles



14 views0 comments

Recent Posts

See All

コメント


bottom of page