1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 | #include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdbool.h>
enum Direction { FORWARD, BACKWARD };
void print_backward_then_forward_1(char* s) {
int n = strlen(s);
for(int i = n - 1; i >= 0; i--) {
fputc(s[i], stdout);
}
for(int i = 0; i < n; i++) {
fputc(s[i], stdout);
}
fputc('\n', stdout);
}
void _print_backward_then_forward_helper(char* s, enum Direction direction) {
int n = strlen(s);
for(int i = 0; i < n; i++) {
int str_idx = (direction == FORWARD ? i : n - i - 1);
fputc(s[str_idx], stdout);
}
}
void print_backward_then_forward_2(char* s) {
_print_backward_then_forward_helper(s, false);
_print_backward_then_forward_helper(s, true);
fputc('\n', stdout);
}
int main(int argc, char* argv[]) {
print_backward_then_forward_1("eyeeros");
printf("------------------\n");
print_backward_then_forward_2("eyeeros");
return EXIT_SUCCESS;
}
/* vim: set tabstop=4 shiftwidth=4 fileencoding=utf-8 noexpandtab: */
|
© Copyright 2019 Alexander J. Quinn This content is protected and may not be shared, uploaded, or distributed.