summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAdam Carpenter <53hornet@gmail.com>2019-05-15 14:28:57 -0400
committerAdam Carpenter <53hornet@gmail.com>2019-05-15 14:28:57 -0400
commit583fc81152e120ec5873f8057f11a894bb8b0a91 (patch)
tree2bea7ada0f2436cf52427e9e767305100468a567
parent3ce6dbf9b9958b44acb1e545fe4fead4aefaf90a (diff)
downloadlearning-c-583fc81152e120ec5873f8057f11a894bb8b0a91.tar.xz
learning-c-583fc81152e120ec5873f8057f11a894bb8b0a91.zip
Finished 1.19 but it can be done in-place.
-rw-r--r--reverse.c12
1 files 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';
}