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

  • برمجة سي بلس بلس
  • برمجة

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

الأجوبة

#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;    
}
هل كان المحتوى مفيد؟

تبحث عن مدرس اونلاين؟

محتاج مساعدة باختيار المدرس الافضل؟ تواصل مع فريقنا الان لمساعدتك بتأمين افضل مدرس
ماهو التخصص الذي تبحث عنه؟
اكتب هنا...