summaryrefslogtreecommitdiff
path: root/reverse.c
diff options
context:
space:
mode:
Diffstat (limited to 'reverse.c')
-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';
}