Open In App

strcat() function in C/C++ with Example

Last Updated : 14 Oct, 2021
Improve
Improve
Like Article
Like
Save
Share
Report

In C/C++, strcat() is a predefined function used for string handling, under string library (string.h in C, and cstring in C++).

This function appends the string pointed to by src to the end of the string pointed to by dest. It will append a copy of the source string in the destination string. plus a terminating Null character. The initial character of the string(src) overwrites the Null-character present at the end of the string(dest).

The behavior is undefined if:

  • the destination array is not large enough for the contents of both src and dest and the terminating null character
  • if the string overlaps.
  • if either dest or src is not a pointer to a null-terminated byte string.

Syntax:

char *strcat(char *dest, const char *src)

Parameters: The method accepts the following parameters:

  • dest: This is a 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.

Return value: The strcat() function returns dest, the pointer to the destination string.

Application: Given two strings src and dest in C++, we need to append string pointed by src to the end of the string pointed by dest.

Examples:

Input: src = "ForGeeks"
       dest = "Geeks"
Output: "GeeksForGeeks"

Input: src = "World"
       dest = "Hello "
Output: "Hello World"

Below is the C program to implement the above approach:

C




// C program to implement
// the above approach
#include <stdio.h>
#include <string.h>
  
// Driver code
int main(int argc,
         const char* argv[])
{
    // Define a temporary variable
    char example[100];
  
    // Copy the first string into
    // the variable
    strcpy(example, "Geeks");
  
    // Concatenate this string
    // to the end of the first one
    strcat(example, "ForGeeks");
  
    // Display the concatenated strings
    printf("%s\n", example);
  
    return 0;
}


Output:

GeeksForGeeks

Note: 
The target string should be made big enough to hold the final string.

My Personal Notes arrow_drop_up

Like Article
Suggest improvement
Next
Share your thoughts in the comments

Similar Reads