Updated March 23, 2023
Introduction to strcat() in C++
C++ is a programming language which includes many functions strcat is one of the inbuilt function of string class which has its own significance like other functions which include strncat, strcpy, etc. These functions play a very pivotal role as they have importance associated with code optimization and function call. strcat() is a function that includes two things in it str refers to the string and cat refers to appending the string either at the end of the string or at the beginning of the string but mostly at the end of the string i.e. appends a copy of the string.
Syntax:
strcat (dest, src);
strcat() basically says to join two strings which means concatenating two strings. Here with the help of this function, we can concatenate two strings but here strcat functions in somewhat different ways. It will append a copy of the source string to the end of the destination string. The strcat() will consider two parameters or arguments.
- dest
- Src
It will work in a way that the copy of the source string will get appended at the end of the destination string.
Return Value: Returned value of the strcat function returns the dest argument, which is pointing to the destination string. If there is a null destination string, then with strcat function there will be overlapping i.e. all characters will get replaced with the copy of source string. One disclaimer to be kept in mind never ever the destination should get overlap only if dest is a null string then only the possibility arises otherwise not. Also, Pointer to the destination location should be large enough so that it can hold or contain a resultant concatenated string.
How to Concatenate String in C++?
Strcat() is the major focus area but still, we will have a peek into the other two ways of concatenating string as they are also part of string inbuild concatenation.
Example #1 – Using strcat() Function
As it is inbuild and predefined function it is mostly associated and defined in “string.h” header file.
Syntax:
char * strcat (char * arr1, char * arr2)
Here, the arr1 and arr2 string should be of character array(char*) and this function concatenates the source or the arr1 string to the end of the arr2 string {destination}.
Code:
# include<bits/stdc++.h>
using namespace std;
int main ()
{
char arr1[] = " An array with src";
char arr2[] = " needs to concatenate with dest";
strcat (arr1, arr2);
cout << arr1 << arr2;
return 0;
}
Output:
Example #2 – Using append() Function
Here str refers to the object which gets instantiated by the string class as a basic string class template that uses char as its character type. The append function helps in adding the string at the end of the text string.
Syntax:
string& string::append (const string& str)
In this example, we will see how source and destination string append with each other.
Code:
# include<bits/stdc++.h>
using namespace std;
int main ()
{
string str1 = " this is a src string";
string str2 = " needs to append with dest";
str1.append(str2);
cout << str1 << str2;
return 0;
}
Output:
Example #3 – Using ‘+’ Operator
This is one of the most convenient ways of concatenating two string. The + operator has the specialty of adding both the strings and then returns a final concatenated string.
Code:
#include<bits/stdc++.h>
using namespace std;
int main ()
{
string str1 = " this is a src string";
string str2 = " needs to appended with dest";
str1 = str1 + str2;
cout << str1 << str2;
return 0;
}
Output:
Although the above three methods are used for string concatenation and using + operator we can easily concatenate two strings with ease compared with both the strings. But our main focus or concern is Using strcat function which is more flexible and readable as it can be called a function any time with overlapping function or at the time of any function call.
Examples to Implement strcat() in C++
Some examples for getting more insights into strcat functions with output:
Example #1
Here we are taking 2 inputs and then we are concatenating both the thing as given below:
Code:
#include <iostream>
#include <string.h>
#include <stdio.h>
using namespace std;
int main ()
{
char stra [100], strb [100];
cout<<"\nEnter first string: ";
gets(stra);
cout<<"\nEnter second string: ";
gets(strb);
strcat (stra, strb);
cout<<"\n\n\t\Resultant first String after concatenation: "<<endl;
cout<<"\n\n\t\t\t"<<stra<<"\n\n\t\t\t";
return 0;
}
Output:
Example #2
Similarly, in this example we are taking 4 inputs and then we are concatenating all the thing as given below:
Code:
#include <stdio.h>
#include <string.h>
int main ()
{
char str [100];
strcpy (str,"concatenate ");
strcat (str,"these ");
strcat (str,"strings ");
strcat (str,"together.");
puts (str);
return 0;
}
Output:
Example #3
Sometimes a basic comparison made with string append and string concatenate as both belong to string.h header files only. Let’s see some examples with strncat() function as well to check the difference between both strncat() and strcat function which belongs to string.h header files.
Code:
#include <stdio.h>
#include <string.h>
int main ()
{
char arr1[80];
char arr2[80];
strcpy (arr1,"To get ");
strcpy (arr2,"or not to get anything");
strncat (arr1, arr2, 30);
puts (arr1);
return 0;
}
Output:
This function also works the same as strcat function with a very minute difference of concatenation and appending of string from src to dest with null characters replacement + n number of character string at the end i.e. with the dest of the end string. Also, the length of the string should be in a way that it should able to accommodate all the appended string at the end of the dest location.
The behavior gets invalidated if it violates the rule which says:
- The strings should not overlap.
- The end i.e. the dest array is not large enough to accommodate final appended content.
Conclusion
Strcat function in C++ is no doubt is a very useful function within the predefined library of string.h header file. Strncat function is also part of string.h header file only but still it has a difference with strcat() in the sense of source and destination of concatenating and appending. It has provided the flexibility to call the function anytime within the program flow or anytime outside the scope of code block as well.
Recommended Articles
This is a guide to strcat() in C++. Here we discuss how to do concatenation using strcat() in C++ along with various examples and code implementation. You can also go through our suggested articles to learn more –