If you are going to perform concatenation in C++, some of the things you must be kept in mind are:
- If a+b is an expression that is showing string concatenation, then the result of the expression will be a copy of the character in ‘a’ followed by the character in ‘b’.
- Either ‘a’ or ‘b’ can be string literal or a value of type char but not both. That’s why the following concatenation doesn’t throw an error but above one does.
Input : "geeks"+"forgeeks" Output : It will not compile, an error will be thrown.
Case 1 : Due to the above reasons, we can not concatenate following expression:
"geeks" + "forgeeks" + geekstring
Here, left associativity of + also plays a role in creating the error as + is left associative so first “geeks” + “forgeeks” will concatenate which will create the error as discussed above.
Case 2 : We can concatenate following:
geekstring + "geeks" + "forgeeks"
Here, left associativity will not create the error as it will join geekstring and “geeks” making it not a literal then “forgeeks” will be added and no error will be generated.
Input : geekstring = "geeks" Input : geekstring + "forgeeks" Output: geeksforgeeks
geeksforgeeks geeksforgeeks Hello
Rated as one of the most sought after skills in the industry, own the basics of coding with our C++ STL Course and master the very concepts by intense problem-solving.
- What happen when we exceed valid range of built-in data types in C++?
- Type difference of character literals in C and C++
- User Defined Literals in C++
- Types of Literals in C/C++ with Examples
- C++ Program to concatenate two strings using Operator Overloading
- Methods to concatenate string in C/C++ with Examples
- Counts of distinct consecutive sub-string of length two using C++ STL
- C++ Program to compare two string using pointers
- Rearrange characters in a string such that no two adjacent are same using hashing
- std::string::replace , std::string::replace_if in C++
- std::string::remove_copy(), std::string::remove_copy_if() in C++
- Check if a string can be formed from another string using given constraints
- Periodic Binary String With Minimum Period and a Given Binary String as Subsequence.
- std::string::crbegin() and std::string::crend() in C++ with Examples
- Check if given string is a substring of string formed by repeated concatenation of z to a
- std::string::replace_copy(), std::string::replace_copy_if in C++
- std::string::append vs std::string::push_back() vs Operator += in C++
- How to sum two integers without using arithmetic operators in C/C++?
- How to quickly swap two arrays of same size in C++?
- Quickly check if two STL vectors contain same elements or not
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.