Last active
March 26, 2022 16:12
-
-
Save dgodfrey206/ce87d22e5d174bc1b1dbe10e057a9c0f to your computer and use it in GitHub Desktop.
Prints string removing tail then adds tail
This file contains hidden or 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
#include <cstring> | |
using namespace std; | |
void downup(const char str[]) { | |
int n = strlen(str); | |
int i, j; | |
for (i = 0; i < n; ++i) { | |
for (j = 0; j < n-i; ++j) | |
cout << str[j]; | |
cout << '\n'; | |
} | |
for (i = 1; i < n; ++i) { | |
for (j = 0; j <= i; ++j) | |
cout << str[j]; | |
cout << '\n'; | |
} | |
} | |
// Recursive version | |
void print(const char* s, int n) { | |
for (int i = 0; i < n; ++i) | |
cout << s[i]; | |
cout << '\n'; | |
} | |
void downup(const char* str, int n) { | |
if (n) { | |
print(str, n); | |
downup(str, n-1); | |
print(str, n); | |
} | |
} | |
int main() { | |
downup("toast"); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment