Created
February 28, 2020 22:03
-
-
Save notsoluckycharm/a9dfeab07b254f707c1a9c5d3dcd799a to your computer and use it in GitHub Desktop.
recursive array join by seperator
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/*(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