strcat() function is used to combine two string together to form a single string in a program.
It concatenates two strings and the final result is returned to the first string. The strcat() function is defined in the header file string.h
.
It takes two arguments, the name of the first string and the second string. These two strings are joined together and the first string stores the final result. And is written as follows:
strcat(first_string, second_string);
C Program for strcat() function
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | #include <stdio.h> #include <string.h> int main() { char str1[20] = "This is "; char str2[20] = "Simple2Code website."; strcat(str1, str2); //str1 stores the concate result printf("After concatenation: %s", str1); return 0; } |
Output:
After concatenation: This is Simple2Code website.
C String – strncat function
It is the same as the above strcat() function but here the difference is that it takes the third argument for n number of characters to be concatenated from the second string to the first string in a program. Like strcat, the result will be stored in the first string.
It is written as follows:
strncat(first_string, second_string, n);
C Program for strncat() function
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | #include <stdio.h> #include <string.h> int main() { char str1[20] = "This is "; char str2[20] = "Simple2Code website"; //4 character are chose from str2 strncat(str1, str2, 4); //Display only This is Simp printf("After concatination: %s", str1); return 0; } |
Output:
After concatenation: This is Simp