From 583fc81152e120ec5873f8057f11a894bb8b0a91 Mon Sep 17 00:00:00 2001 From: Adam Carpenter <53hornet@gmail.com> Date: Wed, 15 May 2019 14:28:57 -0400 Subject: Finished 1.19 but it can be done in-place. --- reverse.c | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/reverse.c b/reverse.c index 0681560..98edd98 100644 --- a/reverse.c +++ b/reverse.c @@ -5,7 +5,7 @@ // Prototypes int get_line(char line[], int maxlength); void line_copy(char to[], char from[]); -void reverse_copy(char to[], char from[]); +void reverse_copy(char to[], char from[], int length); /* * MAIN @@ -17,7 +17,7 @@ int main() { char reversed_line[MAXLENGTH]; // current reversed line while ((len = get_line(line, MAXLENGTH)) > 0) { - reverse_copy(line, reversed_line, len); + reverse_copy(reversed_line, line, len); printf("%s\n", reversed_line); } return 0; @@ -61,8 +61,10 @@ void line_copy(char to[], char from[]) { void reverse_copy(char to[], char from[], int length) { int i; - i = 0; - while ((to[i] = from[i]) != '\0') - ++i; + for (i = 0; i < length; ++i) { + to[length - 1 - i] = from[i]; + } + + to[length] = '\0'; } -- cgit v1.2.3