Q:

Write a C++ program to create a string like "aababcabcd" from a given string "abcd"

0

Write a C++ program to create a string like "aababcabcd" from a given string "abcd"

Sample Output:

aababcabcd
aababc
a

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 str)
        {
            string result = "";
            for (int i = 0; i < str.length(); i++)
            {
                result += str.substr(0, i + 1);
            }
            return result;
        }
        
int main() 
 {
  cout << test("abcd") << endl;  
  cout << test("abc") << endl;  
  cout << test("a") << 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