Q:

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

0

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 

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 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;    
}

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