Skip to content

Instantly share code, notes, and snippets.

@dgodfrey206
Last active March 26, 2022 16:12
Show Gist options
  • Save dgodfrey206/ce87d22e5d174bc1b1dbe10e057a9c0f to your computer and use it in GitHub Desktop.
Save dgodfrey206/ce87d22e5d174bc1b1dbe10e057a9c0f to your computer and use it in GitHub Desktop.
Prints string removing tail then adds tail
#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