Write a C++ program to create a new string of the characters at indexes 0,1, 4,5, 8,9 ... from a given string

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

Write a C++ program to create a new string of the characters at indexes 0,1, 4,5, 8,9 ... from a given string

Sample Output:

Pyon
JaScpt
HT 

الأجوبة

#include <iostream>
 
using namespace std;

string test(string str1)
          {
           string result = "";
            for (int i = 0; i < str1.length(); i += 4)
            {
                int c = i + 2;
                int n = 0;
                n += c > str1.length() ? 1 : 2;
                result += str1.substr(i, n);
            }
            return result;
        }
        
int main() 
 {
  cout << test("Python") << endl; 
  cout << test("JavaScript") << endl; 
  cout << test("HTML") << endl;     
  return 0;    
}
هل كان المحتوى مفيد؟

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

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