C++ Exercises: Create a new string taking 3 characters from the middle of a given string at least 3
C++ Basic Algorithm: Exercise-70 with Solution
Write a C++ program to create a new string taking 3 characters from the middle of a given string at least 3.
Sample Solution:
C++ Code :
#include <iostream>
using namespace std;
string test(string s1)
{
return s1.substr((s1.length() - 1) / 2 - 1, 3);
}
int main()
{
cout << test("Hello") << endl;
cout << test("Python") << endl;
cout << test("abc") << endl;
return 0;
}
Sample Output:
ell yth abc
Pictorial Presentation:
Flowchart:
C++ Code Editor:
Contribute your code and comments through Disqus.
Previous: Write a C++ program to create a new string of length 2 starting at the given index of a given string.
Next: Write a C++ program to create a new string of length 2, using first two characters of a given string. If the given string length is less than 2 use '#' as missing characters.
What is the difficulty level of this exercise?