Q:

Write a C++ program to create a new string of length 2 starting at the given index of a given string

0

Write a C++ program to create a new string of length 2 starting at the given index of a given string

Sample Output:

el
th
on

All Answers

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

#include <iostream>
using namespace std;

string test(string s1, int index)
        {
            return index + 2 <= s1.length() ? s1.substr(index, 2) : s1.substr(0, 2);
        }
         
int main() 
 {
  cout << test("Hello", 1) << endl;  
  cout << test("Python", 2) << endl; 
  cout << test("on", 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