Theme
Primary Color
Lesson 19

Join Elements

محتوى الدرس

#include <iostream>
#include <vector>
using namespace std;

/*
  Join Elements
  {"Elzero", "Web", "School"} -> "Elzero, Web, School"
  {"Elzero", "Web", "School"}, " | " -> "Elzero | Web | School"
*/

string join(vector<string> names, string sep = ", ")
{
  string result;
  for (int i = 0; i < names.size(); i++)
  {
    result += names[i];
    if (i != names.size() - 1)
    {
      result += sep;
    }
  }
  return result;
}

int main()
{
  cout << join({"Elzero", "Web", "School"}) << endl;
  cout << join({"Elzero", "Web", "School"}, " | ") << endl;
  return 0;
}