w3resource

C++ Exercises: Create a new string using two given strings s1, s2, the format of the new string will be s1s2s2s1

C++ Basic Algorithm: Exercise-57 with Solution

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:

C++ Basic Algorithm Exercises: Create a new string using two given strings s1, s2, the format of the new string will be s1s2s2s1.

Flowchart:

Flowchart: Create a new string using two given strings s1, s2, the format of the new string will be s1s2s2s1.

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?



Follow us on Facebook and Twitter for latest update.