C++ Exercises: Concatenate three copies of each string in a given list of strings
Create List of Strings Each Repeated 3 Times
Write a C++ program to create a list from a given list of strings where each element is replaced by 3 copies of the string concatenated together.
Test Data:
Sample Input:
{ "1", "2", "3" , "4" }
Expected Output :
111 222 333 444
Sample Solution:
C++ Code :
#include <bits/stdc++.h>  // Include all standard C++ libraries
#include <list>  // Include the list container
using namespace std;  // Using standard namespace
// Function to create a new list where each element is replaced by three copies of the string concatenated together
list<string> test(list<string> nums) {
    list<string> new_list;  // Declare a new list to store modified elements
    list<string>::iterator it;  // Declare an iterator for traversing the input list 'nums'
    // Iterate through 'nums' list and concatenate three copies of each element together
    for (it = nums.begin(); it != nums.end(); ++it) {
        new_list.push_back(*it + *it + *it);  // Concatenate three copies of the string
    }
    return new_list;  // Return the new list with modified elements
}
// Function to display the elements of a list
display_list(list<string> g) {
    list<string>::iterator it;  // Declare an iterator for traversing the input list 'g'
    // Iterate through 'g' list and display each element
    for (it = g.begin(); it != g.end(); ++it) {
        cout << *it << ' ';  // Print each element followed by a space
    }
    cout << '\n';  // Move to a new line after displaying all elements
}
int main() {
    list<string> text = { "1", "2", "3", "4", "5", "6" };  // Initialize a list 'text' with strings
    cout << "Original list of elements:\n";
    display_list(text);  // Display the original list 'text'
    list<string> result_list;  // Declare a list to store the result of test() function
    result_list = test(text);  // Call the test() function to replace elements with three copies of the string
    cout << "\nNew list from the said list where each element is replaced\nby 3 copies of the string concatenated together:\n";
    display_list(result_list);  // Display the new list with modified elements
    return 0;  // Return 0 to indicate successful completion of the program
}
Sample Output:
Original list of elements: 1 2 3 4 5 6 New list from the said list where each element is replaced by 3 copies of the string concatenated together: 111 222 333 444 555 666
Flowchart:

For more Practice: Solve these Related Problems:
- Write a C++ program to generate a new array from a list of strings where each string is repeated three times consecutively.
- Write a C++ program that reads an array of strings and outputs an array with each element concatenated with itself three times.
- Write a C++ program to process a string array and replace every string with three copies of itself in succession.
- Write a C++ program that takes an array of strings and prints each string repeated three times without any spaces in between.
Go to:
PREV : Add "$" at Start and End of Each String in Array. 
NEXT : Add 3 and Multiply by 4 for Each Integer in Array.
C++ Code Editor:
Contribute your code and comments through Disqus.
What is the difficulty level of this exercise?
