In this post, we are going to explore the strcat function in C++ along with its syntax, parameters, functionality, and illustrations.
What is Strcat Function?
Joining two strings together is achieved using the fundamental string manipulation function strcat in C++.
Syntax:
It has the following syntax:
The function char strcat(char destination, const char* source); concatenates two strings and appends the contents of the source string to the destination string.
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:
Upon completion of the concatenation process, the function provides a reference to the resulting string, representing the combined strings.
In the target string, the function initially examines 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.
- 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.
Key Points
Memory Management: Developers are tasked with the responsibility of managing memory when using strcat, which operates on character arrays or C-style strings. This entails ensuring that memory is allocated correctly for both the source and destination strings, and that memory access is restricted within the specified boundaries.
Example:
Let's consider a scenario to demonstrate the strcat function in C++.
#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:
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: ".