C++ Exercises: Create a new string using three copies of the last two character of a given string of length atleast two
C++ Basic Algorithm: Exercise-59 with Solution
Write a C++ program to create a new string using three copies of the last two character of a given string of length atleast two.
Sample Solution:
C++ Code :
#include <iostream>
using namespace std;
string test(string s1)
{
string last2 = s1.substr(s1.length() - 2);
return last2 + last2 + last2;
}
int main()
{
cout << test("Hello") << endl;
cout << test("Hi") << endl;
return 0;
}
Sample Output:
lololo HiHiHi
Pictorial Presentation:
Flowchart:
C++ Code Editor:
Contribute your code and comments through Disqus.
Previous: Write a C++ program to insert a given string into middle of the another given string of length 4.
Next: Write a C++ program to create a new string using first two characters of a given string. If the string length is less than 2 then return the original string.
What is the difficulty level of this exercise?