C++ Exercises: Create a new string using the two middle characters of a given string of even length
C++ Basic Algorithm: Exercise-67 with Solution
Write a C++ program to create a new string using the two middle characters of a given string of even length (at least 2).
Sample Solution:
C++ Code :
#include <iostream>
using namespace std;
string test(string s1)
{
return s1.substr(s1.length() / 2 - 1, 2);
}
int main()
{
cout << test("Hell") << endl;
cout << test("JS") << endl;
return 0;
}
Sample Output:
el JS
Pictorial Presentation:
Flowchart:
C++ Code Editor:
Contribute your code and comments through Disqus.
Previous: Write a C++ program to create a new string without the first and last character of a given string of any length.
Next: Write a C++ program to create a new string using the first and last n characters from a given string of length at least n.
What is the difficulty level of this exercise?