C++ Exercises: Create a new string without the first and last character of a given string of any length
C++ Basic Algorithm: Exercise-66 with Solution
Write a C++ program to create a new string without the first and last character of a given string of any length.
Sample Solution:
C++ Code :
#include <iostream>
using namespace std;
string test(string s1)
{
return s1.length() < 2 ? " " : s1.substr(1, s1.length() - 2);
}
int main()
{
cout << test("Hello") << endl;
cout << test("JS") << endl;
return 0;
}
Sample Output:
ell
Pictorial Presentation:

Flowchart:

C++ Code Editor:
Contribute your code and comments through Disqus.
Previous: Write a C++ program to move the first two characters to the end of a given string of length at least two.
Next: Write a C++ program to create a new string using the two middle characters of a given string of even length (at least 2).
What is the difficulty level of this exercise?
- New Content published on w3resource:
- Scala Programming Exercises, Practice, Solution
- Python Itertools exercises
- Python Numpy exercises
- Python GeoPy Package exercises
- Python Pandas exercises
- Python nltk exercises
- Python BeautifulSoup exercises
- Form Template
- Composer - PHP Package Manager
- PHPUnit - PHP Testing
- Laravel - PHP Framework
- Angular - JavaScript Framework
- React - JavaScript Library
- Vue - JavaScript Framework
- Jest - JavaScript Testing Framework