Last updated on July 27, 2020
This syntax of the strcat() function is:
Syntax:char* strcat (char* strg1, const char* strg2);
This function is used to concatenate two strings. This function accepts two arguments of type pointer tochar or(char*), so you can either pass a string literal or an array of characters. The null character from the first string is removed then second string is appended at the end of the first string. It returns a pointer to the resulting string (strg1). Generally, the return value ofstrcat() is discarded.
Here are some examples:
1 2 3 4 5 6 7 8 910111213 | charstrg1[40]="Hello";/*returns a pointer (which is discarded) to the string literal"Hello World" and now strg1 contains "Hello World"*/strcat(strg1," World");/*returns a pointer (which is discarded) to the stringto "Hello World :)" and now strg1 contains "Hello World :)"*/strcat(strg1," :)"); |
You should never pass a string literal as a first argument because if you do thenstrcat() function will try to modify a string literal which is an undefined behavior and may cause the program to crash.
strcat("Yello"," World");// wrong
The behaviour ofstrcat() is undefined when the size of the array pointed to bystrg1 isn't long enough to accommodate all the characters fromstrg2. It is programmer's responsibility to make sure that size of the array pointed to bystrg1 is long enough to accommodate all the characters fromstrg2.
The following program demonstrates how to usestrcat() function.
1 2 3 4 5 6 7 8 91011121314151617181920212223 | #include<stdio.h>#include<string.h>intmain(){charstrg1[40];charstrg2[40];printf("Enter first string: ");gets(strg1);printf("Enter second string: ");gets(strg2);printf("\nConcatenating first and second string ..\n\n");strcat(strg1,strg2);printf("First string: %s\n",strg1);printf("Second string: %s",strg2);// signal to operating system program ran finereturn0;} |
Expected Output:
1234567 | Enter first string: topEnter second string: potConcatenating first and second string ..First string: toppotSecond string: pot |
Let's create our own version ofstrcat() function.
1 2 3 4 5 6 7 8 910111213141516171819 | char*my_strcat(char*strg1,char*strg2){char*start=strg1;while(*strg1!='\0'){strg1++;}while(*strg2!='\0'){*strg1=*strg2;strg1++;strg2++;}*strg1='\0';returnstart;} |
How it works:
Themy_strcat() function accepts two arguments of type pointer tochar or(char*) and returns a pointer to the first string i.estrg1.
In line 3, we have assigned the pointerstrg1 tostart, this step is necessary otherwise we will lose track of the address of the beginning of the first string (strg1).
The job of the first while is loop is to move the pointerstrg1 to the last character i.e'\0'. So that the second while loop can begin appending character at this position.
The second while loop appends character one by one from second string to first string. Since after first while loopstrg1 is pointing to the null character of the first string, in the first iteration the statement:
*strg1=*strg2;
appends first character from second string to the end of the first string (i.e in place of null character'\0'). Thenstrg1++ andstrg2++ is incremented. This process keeps repeating until the null character is encountered in the second string (strg2).
At this point, the string pointed to by start still lacks one thing, the null character ('\0'). The statement:
*strg1='\0';
appends the null character at the end of the first string.
At last, thereturn statement returns the pointer to the first string to the calling function.