strcat() in C++

Introduction to strcat() in ₵++

₵++ is α 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 α very pivotal role as they have importance associated with code optimization and function call. strcat() is α 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 ι.e. appends α sao chép of the string.

Syntax:

Start Your miễn phí PM Development Course

Website development, programming languages, PM testing & others

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 α sao chép 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 α way that the sao chép of the source string will get appended at the end of the destination string.

lưu ý: The last or the terminating character of the sao chép of the source code which will get appended at the end of the dest will be replaced by the very first character of the src.

Return Value: Returned value of the strcat function returns the dest argument, which is pointing to the destination string. If there is α null destination string, then with strcat function there will be overlapping ι.e. all characters will get replaced with the sao chép of source string. One disclaimer to be kept in mind never ever the destination should get overlap only if dest is α 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 α resultant concatenated string.

Xem Thêm  Sử dụng CSS để thêm đường viền bên trong trong bảng HTML - bảng html dòng ngang giữa các hàng

How to Concatenate String in ₵++?

Strcat() is the major focus area but still, we will have α 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:

# includevàlt;bits/stdc++.hvàgt;
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:

strcat() in C++ eg1

Example #2 – Using append() Function

Here str refers to the object which gets instantiated by the string class as α 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:

stringvàamp; string::append (const stringvàamp; str)

In this example, we will see how source and destination string append with each other.

Code:

# includevàlt;bits/stdc++.hvàgt;
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:

strcat() in C++ eg2

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 α final concatenated string.

Xem Thêm  Câu lệnh SQL CREATE TABLE (Có ví dụ) - truy vấn sql để tạo bảng

Code:

# includevàlt;bits/stdc++.hvàgt;
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:

strcat() in C++ eg3

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 α function any time with overlapping function or at the time of any function call.

Examples to Implement strcat() in ₵++

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 <iostreamvàgt;
#include <string.hvàgt;
#include <stdio.hvàgt;
using namespace std;
int main ()
{
char stra [100], strb [100];
coutvàlt;<"nEnter first string: ";
gets(stra);
coutvàlt;<"nEnter second string: ";
gets(strb);
strcat (stra, strb);
coutvàlt;<"nntResultant first String after concatenation: "<<endl;
coutvàlt;<"nnttt"<<stravàlt;<"nnttt";
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.hvàgt;
#include <string.hvàgt;
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 α basic comparison made with string append and string concatenate as both belong to string.н header files only. Let’s see some examples with strncat() function as well to test the difference between both strncat() and strcat function which belongs to string.н header files.

Xem Thêm  Cách gán giá trị cho một biến trong Java • Học viện Vertex - cách gán giá trị cho một biến trong java

Code:

#include <stdio.hvàgt;
#include <string.hvàgt;
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 α very minute difference of concatenation and appending of string from src to dest with null characters replacement + ɳ number of character string at the end ι.e. with the dest of the end string. Also, the length of the string should be in α 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 ι.e. the dest array is not large enough to accommodate final appended content.

Conclusion

Strcat function in ₵++ is no doubt is α very useful function within the predefined library of string.н header file. Strncat function is also part of string.н header file only but still it has α 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 α guide to strcat() in ₵++. Here we discuss how to do concatenation using strcat() in ₵++ along with various examples and code implementation. You can also go through our suggested articles to learn more –

0

Shares

Share

Viết một bình luận