This function is a concatenation string function. strcat() function concatenates the destination string at the end of the source string. This means it connects two strings. Destination String is appended at the end of source String.
Here in below example:
Source String is: src
Destination String is: city
Syntax for strcat():
1 |
strcat(source,destination); |
Example:
1 2 3 4 5 6 7 8 9 10 11 |
#include<stdio.h> #include<string.h> #include<ctype.h> int main() { char src[]="Bombay"; char city[]="+Nagpur"; printf("Before concatenation destination string = %s\n", src ) ; strcat(src,city); printf("After concatenation destination string = %s", src ) ; } |
Output:
1 2 |
Before concatenation destination string = Bombay After concatenation destination string = Bombay+Nagpur |
C strncat()
This strncat() function is used when u need to concatenate some portion of one string
at the end of another string. Here we can give a number of characters that have to concatenate.
Syntax for strncat()
1 |
strncat(source,destination,number_of_character) |
Example:
1 2 3 4 5 6 7 8 9 10 11 |
#include<stdio.h> #include<string.h> #include<ctype.h> int main() { char src[] = "Bombay"; char city[20] = "+Nagpur"; printf("Before concatenation destination string = %s\n", src ) ; strncat(src, city, 3); printf("After concatenation destination string = %s", src ) ; } |
Output:
1 2 |
Before concatenation destination string = Bombay After concatenation destination string = Bombay+Na |
In the example there are 3 characters from destination string is append to source string.