C++ Exercises: Create a new string using two given strings s1, s2, the format of the new string will be s1s2s2s1
String Format s1s2s2s1
Write a C++ program to create another string using two given strings s1, s2, the format of the new string will be s1s2s2s1.
Sample Solution:
C++ Code :
#include <iostream>
using namespace std;
// Function 'test' concatenates strings and creates a new string based on the input
string test(string s1, string s2)
{
return s1 + s2 + s2 + s1; // Concatenates s1, s2, s2, and s1 in the given order
}
// Main function to test the 'test' function
int main()
{
// Displays the output of the 'test' function for different string inputs
cout << test("Hi", "Hello") << endl; // Output: "HiHelloHelloHi" (concatenation of strings)
cout << test("whats", "app") << endl; // Output: "whatsappappwhats" (concatenation of strings)
return 0;
}
Sample Output:
HiHelloHelloHi whatsappappwhats
Visual Presentation:

Flowchart:

For more Practice: Solve these Related Problems:
- Write a C++ program to concatenate two strings into a new string in the format s1 + s2 + s2 + s1.
- Write a C++ program that reads two strings and constructs a new string by sandwiching s2 between two copies of s1.
- Write a C++ program to generate a patterned string where the second string is duplicated and placed between two instances of the first string.
- Write a C++ program that accepts two inputs and outputs a combined string in the order s1, s2, s2, s1, ensuring proper concatenation.
C++ Code Editor:
Contribute your code and comments through Disqus.
Previous: Write a C++ program to check three given integers (small, medium and large) and return true if the difference between small and medium and the difference between medium and large is same.
Next: Write a C++ program to insert a given string into middle of the another given string of length 4.
What is the difficulty level of this exercise?