Skip to content

Instantly share code, notes, and snippets.

@notsoluckycharm
Created February 28, 2020 22:03
Show Gist options
  • Save notsoluckycharm/a9dfeab07b254f707c1a9c5d3dcd799a to your computer and use it in GitHub Desktop.
Save notsoluckycharm/a9dfeab07b254f707c1a9c5d3dcd799a to your computer and use it in GitHub Desktop.
recursive array join by seperator
/*(Print an array) Write a recursive method that display all the
elements in an array of intergers, separated by spaces. */
#include<iostream>
#include<string>
using namespace std;
string recursiveJoin(int arr[], string seperator, int size, int index=0)
{
if (index == size - 1) {
return std::to_string(arr[index]);
}
else {
return std::to_string(arr[index])
.append(seperator)
.append(recursiveJoin(arr, seperator, size, index + 1));
}
}
int main()
{
int arrayOfNumbers[] = { 1, 2, 3, 4, 5, 6 };
int arraySize = std::size(arrayOfNumbers);
cout << recursiveJoin(arrayOfNumbers, " ", arraySize);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment