Q:

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

0

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

Sample Output:

Ho
Pyon
on
oo

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

#include 

using namespace std;

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

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Similar questions


need a help?


find thousands of online teachers now