strcat()
is a useful string library function, which concatenates one string to another.
To concatenate is to glue one or more pieces of data together or to connect one or more links together.
Syntax
Following is the syntax of strcat() function:
1 |
char *strcat(char *dest, const char *src) |
Parameters
dest
− This is pointer to the destination array, which should contain a C string, and should be large enough to contain the concatenated resulting string.
src
− This is the string to be appended. This should not overlap the destination.
Example of strcat()
Let’s see the simple example to concatenate first string with another:
1 2 3 4 5 6 7 8 9 10 11 12 13 |
#include<stdio.h> #include<string.h> int main() { char ch[20]={'h', 'e', 'l', 'l', 'o', '\0'}; char ch2[20]={'w', 'o', 'r', 'l', 'd', '\0'}; strcat(ch,ch2); printf("Value of first string is: %s",ch); return 0; } |
1 |
Value of first string is: helloworld |
String Manipulation Functions
Few commonly used string handling functions are discussed below:
String Functions | Description |
---|---|
strlen() | Calculates the length of string |
strcat() | Concatenates(joins) two strings |
strncat() | Appends a portion of string to another |
strcpy() | Copies a string to another string |
strncpy() | Copies number of characters of one string to another |
strcmp() | Compares two strings |
strncmp() | compares at most the first n bytes of str1 and str2. |
strchr() | Returns pointer to first occurrence of char |
strrchr() | Return last occurrence of given character in a string is found |
strstr() | Returns pointer to the first occurrence of the string |
strdup() | Duplicates the string |
strndup() | Similar to strdup(), but copies at most n bytes. |
strerror() | Returns the error message |
strtok() | Tokenizing given string using delimiter |
strcoll() | Compares first string with another. |