From da9de48bac8b6c3fb39945a50232e3c7132ba82e Mon Sep 17 00:00:00 2001 From: Kris Lamoureux Date: Thu, 2 Dec 2021 20:29:00 -0500 Subject: [PATCH] Exercise 1-19. Write a function reverse(s) --- 14-char-arrays.c | 20 ++++++++++++++++---- 1 file changed, 16 insertions(+), 4 deletions(-) diff --git a/14-char-arrays.c b/14-char-arrays.c index bcd8fa5..cf6e27d 100644 --- a/14-char-arrays.c +++ b/14-char-arrays.c @@ -1,10 +1,11 @@ #include #define MAXLINE 1000 /* maximum input line size */ -#define MINLINE 81 +#define MINLINE 0 int getline(char line[], int maxline); void copy(char to[], char from[]); void trim(char line[], int len); +void reverse(char to[], char from[], int len); /* print longest input line */ main() @@ -13,7 +14,7 @@ main() int max; int c; char line[MAXLINE]; - char longest[MAXLINE]; + char enil[MAXLINE]; max = 0; while((len = getline(line, MAXLINE)) > 0) { @@ -24,8 +25,8 @@ main() ++len; } if (len >= MINLINE) { - trim(line, len); - printf("%s", line); + reverse(enil, line, len); + printf("%s", enil); } } return 0; @@ -67,3 +68,14 @@ void trim(char s[], int len) else return; } + +void reverse(char to[], char from[], int len) +{ + int i; + + i = 0; + while ((to[i] = from[len-1]) != '\0') { + ++i; + --len; + } +}