Updated March 23, 2023
Introduction to strcat() in C
The strcat() function in c is usually used for the string concatenation in the programming process. strcat() concatenates a specific string with another string. It is a built-in function. Strcat() built-in function in c will only work without any error if you define <string.h> in the header file/position of the Project/Program.
Syntax:
Char *strcat(char *str1, const char *str2)
Parameters:
The above built-in function ( strcat() ) has/takes just 2 arguments which may be two strings/the character arrays. Strings that are concatenated will be stored in the first string itself in the argument.
- Str1: destination string’s pointer.
- Str2: source string’s pointer which can be appended/added to the destination string.
Examples to Implement strcat() in C
Below are the different examples:
Example #1
Program to concatenate the strings. Here stra1, stra2 already assigned with the string values by assigning.
Code:
#include <stdio.h>
#include <string.h>
int main()
{
char stra1[] = "This is ", stra2[] = "programiz.com";
//concatenates stra1 and stra2 and resultant string is stored in str1.
strcat(stra1,stra2);
puts(stra1);
puts(stra2);
return 0;
}
Output:
Example #2
Program to concatenate stra1 and stra2 string’s variable values.
Code:
#include <stdio.h>
#include <string.h>
int main () {
char stra1[1000], stra2[1000];
//stra1 destination string
strcpy(stra1, "Hi I m Pavan Kumar Sake . This is my 1st string to append to other string , \n");
//stra2 source string
strcpy(stra2, " (This is my 2nd string to concatenate and then to print the result from the 1st string variable )\n" );
//concatenating the string stra2 to the string stra1
strcat(stra1, stra2);
//stra1 : displaying destination string
printf("String after concatenation: %s", stra1);
return(0);
}
Output:
Example #3
Program of Concatenation after copying the specific string to the stra1, stra2.
Code:
#include <stdio.h>
#include <string.h>
int main () {
char stra1[1000], stra2[1000];
//stra1 destination string
strcpy(stra1, "Hi I m Pavan Kumar Sake . This is my 1st string \n to append to other string , ");
//stra2 source string
strcpy(stra2, "(This is my 2nd string to concatenate and then to print \n the result from the 1st string variable )" );
//concatenating the string stra2 to the string stra1 in the printf statement itself
printf("String after concatenation: %s", strcat(stra1, stra2));
//The resultant of the concatenated string usually stores in stra1 but here strcat() used in the print statement itself
//so no need to declare stra1 again
return(0);
}
Output:
Same output as the above example’s output due to the same programming logic except for the declaration of the strcat() position. The same strings are used just like the above.
Example #4
Program of concatenation in the print statement itself in C Programming Language.
Code:
#include <stdio.h>
#include <string.h>
int main () {
//Assigning strings to the variables
char stra1[1000] = "Hello I m Pavan Kumar Sake . This is my 1st string to append to other string , \n";
char stra2[1000] = " (This is my 2nd string to concatenate and then to print the result from the 1st string variable )";
//concatenating the string stra2 to the string stra1 in the printf statement itself
printf("String after concatenation: %s", strcat(stra1, stra2));
//The resultant of the concatenated string usually stores in stra1 but here strcat() used in the print statement itself
//so no need to declare stra1 again
return(0);
}
Output:
Example #5
C Program to show concatenation of 2 strings using 2 different types i.e., normal & also by shifting the source and the target strings. Check the c program below, you will know.
Code:
#include <stdio.h>
#include <string.h>
int main( )
{
char source1[ ] = " Best one" ;
//assigning the string to the source1 string
char target1[ ]= " Java tutorial" ;
//assigning the string to the target1 string
printf ( "\nSource string 1st one = %s ", source1 ) ;
//printing the source1 value
printf ( "\nTarget string 2nd one = %s \n", target1 ) ;
//printing the target value
strcat ( target1, source1 ) ;
//concatenating the target1 and the source1 values/strings
printf ( "\n Target string after strcat( ) = %s \n ", target1 ) ;
//target1 will now have the concatenated and then it will be printed
printf ( "\n Source string after strcat( ) = %s \n ", strcat(source1, target1) ) ;
//source 1 doesnot have any value because already string appended and the resultant stored in the target string/chars
}
Output:
Example #6
Program of concatenation using the define variables and with the NULL CHARACTER.
Code:
#include <stdio.h>
#include <string.h>
//Program to Concatenate the NULL STRING
#define DEST_SIZE1 40
// defining the dest_size1 of 40
char dest1[DEST_SIZE1];
//assigning the dest with dest size
int main()
{
strcat(dest1, "Look Here");
//concatenating the null string/character
printf(dest1);
//Printing the dest1 value
return 0;
}
Output:
Example #7
Program of concatenation using Pointers.
Code:
#include <stdio.h>
#include <string.h>
//String Character Program with the pointers used
#define DEST_SIZE1 40
int main()
{
char src1[] = "Look Here";
//assigning string to the src1 variable
char dest1[DEST_SIZE1] = "Unimaginable";
//assigning string to the dest1 variable
char *ps1 = src1 + 4;
//assigning src1 string variables value to the pointer ps1
char *pd1 = dest1 + 6;
//assigning dest1 string variables value to the pointer pd1
strcat(pd1, ps1);
//concatenating the pointers
printf(dest1);
//printing dest1 value
strcat(src1, dest1);
//concatenating the src1 value by concatenating the src1 and dest1
printf("\n\n");
//line breaks
printf(src1);
//printing src1 value - it is the concatenation value
return 0;
}
Output:
Example #8
This is the program of strcat()’s similar function strncat(). It is very helpful to concatenate only the particular length of the characters from the string.
Code:
#include <stdio.h>
#include <string.h>
//This is a program of strncat() function to concatenate only first 4 characters of the string.
#define DEST_SIZE1 40
//defining the dest_size1 with 40 value
int main()
{
char src1[] = " Hey Buddy!!! ";
//assigning the string to the src character's string - 1st string
char dest1[DEST_SIZE1] = "How are you";
//assigning the string value to the dest[40] because dest_size1 already assigned - 2nd string
strncat(dest1, src1, 4);
//concatenating the dest, src with the condition of concatenating only 4 strings of src1
printf(dest1);
//printing the dest1 value because now the dest1 now has concatenated string
return 0;
}
Output:
Conclusion
This is about the concept of strcat() in C Programming Language and how strcat() function should be declared, strcat() works in C i.e., how strcat() function will append/ concatenate the 2 strings. All outputs with some examples of strcat are listed above.
Recommended Articles
This is a guide to strcat() in C. Here we discuss the syntax, Parameters and top examples of strcat() in C along with code implementation. You may also look at the following articles to learn more-