w3resource

C++ Exercises: Create a new string without the first and last character of a given string of length atleast two

C++ Basic Algorithm: Exercise-62 with Solution

Write a C++ program to create a new string without the first and last character of a given string of length atleast two.

Sample Solution:

C++ Code :

#include <iostream>
using namespace std;

string test(string s1)
            {
             return s1.substr(1)
                .substr(0, s1.length() - 2);
         }
         
int main() 
 {
  cout << test("Hello") << endl;  
  cout << test("Hi") << endl; 
  cout << test("Python") << endl;   
  return 0;    
}

Sample Output:

ell

ytho

Pictorial Presentation:

C++ Basic Algorithm Exercises: Create a new string without the first and last character of a given string of length atleast two.

Flowchart:

Flowchart: Create a new string without the first and last character of a given string of length atleast two.

C++ Code Editor:

Contribute your code and comments through Disqus.

Previous: Write a C++ program to create a new string of the first half of a given string of even length.
Next: Write a C++ program to create a new string from two given string one is shorter and another is longer. The format of the new string will be long string + short string + long string.

What is the difficulty level of this exercise?