Strcat Function In C++

In this article, we will discuss the strcat function in C++ with its syntax, parameters, operation, and examples.

What is Strcat Function?

The concatenating two strings is accomplished with the help of the basic string manipulation function strcat in C++.

Syntax:

It has the following syntax:

char strcat(char destination, const char* source);

Parameters:

  • destination: It is a pointer to the array at the destination, which needs to be large enough to accommodate the resultant string that has been concatenated.
  • source: It is a pointer to the string that has to be appended. The destination string has this string added to the end.
  • Return Value:

After the concatenation process, the function returns a pointer to the destination string, which is the concatenated string.

Operation:

  • In the destination string, the function checks first for the null terminator ('\0').

This null terminator indicates the end of the string.

  • After that, characters from the source string are copied to the destination string's place just after the null terminator.
  • This process appends the source string to the end of the destination string until the null terminator of the source string is reached.
  • In the end, it makes sure the string is ended correctly by adding a null terminator ('\0') to the end of the concatenated string.
  • Key Points

  • Buffer Overflow: The destination array must have sufficient space to store the concatenated string. If the destination buffer is not large enough to hold the concatenated string, buffer overflow might happen, which may result in unexpected behaviour and security issues.
  • String Termination: Both the source and destination strings must be correctly null-terminated C-style strings for strcat to function properly. If not, the behaviour is undefined.

Memory Management: Programmers are in responsible for memory management as strcat makes use of character arrays, or C-style strings. It means that memory must be allocated properly for both source and destination strings, and memory cannot be accessed outside of boundaries.

Example:

Let us take an example to illustrate the strcat method in C++.

Example

#include <iostream>
#include <cstring>
int main() 
{
    // Case-1: Where Destination string is large enough.
    char destination_1[20] = "Hello ";
    char source_1[] = "India!";
    strcat(destination_1, source_1);
    std::cout << "Case-1: Destination is large enough: " << destination_1 << std::endl;
    // Case-2: Where Destination string is just enough.
    char destination_2[11] = "Hello ";
    char source_2[] = "India!";
    strcat(destination_2, source_2);
    std::cout << "Case-2: Destination is just enough: " << destination_2 << std::endl;
    // Case-3: Where Destination string is too small.
    char destination_3[10] = "Hello ";
    char source_3[] = "India!";
    strcat(destination_3, source_3);
    std::cout << "Case-3: Destination is too small: " << destination_3 << std::endl;
    // Case-4: Source string is empty.
    char destination_4[20] = "Hello ";
    char source_4[] = "";
    strcat(destination_4, source_4);
    std::cout << "Case-4: Source is empty: " << destination_4 << std::endl;
    // Case-5: Both destination and source strings are empty.
    char destination_5[10] = "";
    char source_5[] = "";
    strcat(destination_5, source_5);
    std::cout << "Case-5: Both destination and source are empty: " << destination_5 << std::endl;
    return 0;
}

Output:

Output

Case-1: Destination is large enough: Hello India!
Case-2: Destination is just enough: Hello India!
Case-3: Destination is too small: Hello India!
Case-4: Source is empty: Hello 
Case-5: Both destination and source are empty:

Explanation:

  • In Case-1, the destination1 has enough room to fit the string "Hello India!" concatenated. The "Case-1: Destination is large enough: Hello India!" is the output that is produced when strcat successfully concatenates source1 and destination_1.
  • Case-2 demonstrates that destination2 including the null terminator barely fits the concatenated string. When source2 and destination_2 are concatenated using strcat, the output "Case-2: Destination is just enough: Hello India!" indicates that the concatenation was successful.
  • In Case-3, the concatenated string "Hello India!" cannot fit in destination3 due to its small size. As a result of buffer overflow and undefinable behaviour, strcat tries to concatenate source3 with destination_3. The result "Case-3: Destination is too small: Hello India!" shows how the concatenated string was truncated since there wasn't sufficient space for it.
  • The case where the source4 string is empty is demonstrated in Case-4. Upon concatenating an empty string to destination4, strcat keeps destination4 The destination4 value is displayed as it is in the output "Case-4: Source is empty: Hello".
  • Finally, Case-5 has empty strings for both source5 and destination5. An empty string is the result of concatenating two empty strings with the function strcat. An empty string is kept in destination_5, which is displayed in the output "Case-5: Both destination and source are empty: ".

Input Required

This code uses input(). Please provide values below: