The C Programming Language, 2nd Edition, by Kernighan and Ritchie
Exercise 1.19 on page 31
Write a function reverse(s)
that reverses the character string s
. Use it to write a program that reverses its input a line at a time.
Solution by Vidhan Gupta
/* Write a function reverse(s) that reverses the character string s. Use it to write a program that reverse its input a line at a time. */ #include <stdio.h> #define MAXIMUM 1000 int getLine(char s[], int lim); void reverse(char line[], int len); int main() { int len; char line[MAXIMUM]; while ((len = getLine(line, MAXIMUM)) > 0) { reverse(line,len); printf("%s", line); } return 0; } int getLine(char s[], int lim) { int i, c; for (i = 0; i < lim - 1 && (c = getchar()) != EOF && c != '\n'; ++i) s[i] = c; if (c == '\n') { s[i] = c; ++i; } s[i] = '\0'; return i; } void reverse(char line[], int len){ int i, j; char temp; for(i = 0, j = len-1 ; i<j; ++i){ temp = line[i]; line[i] = line[j]; line[j] = temp; j--; } }
Console Window: What the hell? ?lleh eht tahW The hell is this. .siht si lleh ehT
Solution by Miguel Degrossoli
#include <stdio.h> /* Exercise 1-19. Write a function "reverse(s)" that reverses the character * string "s". Use it to write a program that reverses its input a line at a * time. */ #define MAXLEN 81 /* maximum length of the string */ void reverse(char s[]); int main() { int c, i; char line[MAXLEN]; i = 0; while ((c = getchar()) != EOF) { line[i] = c; i++; if (c == '\n' || i == (MAXLEN - 1)) { line[i-1] = '\0'; reverse(line); i = 0; } } } void reverse(char s[]) { int i, n; char line[MAXLEN]; for (i = 0; s[i] != '\0' && i < MAXLEN; ++i) ; n = 0; if (i == 0 || s[i] == '\0') line[i] = '\0'; if (i > 0) for (--i; i >=0; --i) { line[i] = s[n]; n++; } printf("%s\n", line); }
Solution by Solution by Richard Heathfield
#include <stdio.h> #define MAX_LINE 1024 void discardnewline(char s[]) { int i; for(i = 0; s[i] != '\0'; i++) { if(s[i] == '\n') s[i] = '\0'; } } int reverse(char s[]) { char ch; int i, j; for(j = 0; s[j] != '\0'; j++) { } --j; for(i = 0; i < j; i++) { ch = s[i]; s[i] = s[j]; s[j] = ch; --j; } return 0; } int getline(char s[], int lim) { int c, i; for(i = 0; i < lim - 1 && (c = getchar()) != EOF && c != '\n'; ++i) { s[i] = c; } if(c == '\n') { s[i++] = c; } s[i] = '\0'; return i; } int main(void) { char line[MAX_LINE]; while(getline(line, sizeof line) > 0) { discardnewline(line); reverse(line); printf("%s\n", line); } return 0; }
Solution by Amarendra Godbole
#include <stdio.h> #define MAXLINE 1000 /* reverse a line, discard empty lines */ int getline(char s[], int max); void reverse(char s[]); int main(void) { int len, i; char line[MAXLINE], longest[MAXLINE]; while ((len = getline(line, MAXLINE)) != 0) { if (len > 1) { reverse(line); printf("%s\n", line); } } return 0; } int getline(char s[], int max) { int i, c; for (i=0; i<max-1 && (c=getchar())!=EOF && c!='\n'; ++i) { s[i] = c; } if (c == '\n') { s[i] = c; ++i; } s[i] = '\0'; return i; } void reverse(char s[]) { int i, j; char temp; for (j = 0; s[j] != '\0'; ++j) ; --j; if (s[j] == '\n') { s[j] = '\0'; --j; } for (i = 0; i < j; i++) { temp = s[i]; s[i] = s[j]; s[j] = temp; --j; } }
Solution by Pilcrow
>h.oidts< edulcni# 4201 ENILXAM enifed# )mil tni ,][s rahc(enilteg tni { ;i ,c tni )i++ ;'n\' =! c && FOE =! ))(rahcteg=c( && 1-mil < i ;0 = i( rof ;c = ]i[s )'n\' == c( fi ;c = ]++i[s ;'0\' = ]i[s ;i nruter } )nel tni ,][taht rahc(esrever diov { ;y ,x tni ;t rahc )'n\' == ]1-nel[taht(fi ;'0\' = ]nel--[taht { )++y ,--x ;y>x ;0=y ,1-nel = x(rof ;]x[taht = t ;]y[taht = ]x[taht ;t = ]y[taht } ;nruter } )diov(niam tni { ;]ENILXAM[s rahc ;nel tni ;x tni { )0 > ))ENILXAM ,s(enilteg = nel((elihw ;)nel ,s(esrever ;)s,"n\s%"(ftnirp } ;0 nruter }
Solution by scopych
#include <stdio.h> #define MAXLINE 1000 /* maximum input line length */ int mygetline ( char line[], int maxline ); /* revers string */ int main() { char line[MAXLINE], revline[MAXLINE]; int i, ii, len; while ((len = mygetline(line, MAXLINE)) > 0){ ii = 0; for (i = len - 2; i >= 0; --i){ /* len-2 to cut off \n\0 at the end of a line */ revline[ii] = line[i]; ii++; } revline[ii++] = '\n'; revline[ii++] = '\0'; printf("%s", revline); } return 0; } /* mygetline: read a line into s, return length */ int mygetline(char s[], int lim) { int c, i; for (i=0; i < lim-1 && (c=getchar() ) !=EOF && c!='\n'; ++i) s[i] = c; if (c == '\n') { s[i] = c; ++i; } s[i] = '\0'; return i; }
And more correct variant 27 jun 2017
#include <stdio.h> #define MAXLINE 1000 /* maximum input line length */ int mygetline ( char line[], int maxline ); int revstringf(char line[], char revline[]); /* revers string using revstringf function */ int main() { char line[MAXLINE]; char revline[MAXLINE]; while ((mygetline(line, MAXLINE)) > 0){ revstringf(line, revline); printf("%s", revline); } return 0; } /* mygetline: read a line into s, return length */ int mygetline(char s[], int lim) { int c, i; for (i=0; i < lim-1 && (c=getchar() ) !=EOF && c!='\n'; ++i) s[i] = c; if (c == '\n') { s[i] = c; ++i; } s[i] = '\0'; return i; } /* revstringf: reverses string s */ int revstringf(char s[], char revline[]) { int i, ii; i = 0; while(s[i] != '\n') /* find number of last character in a string */ ++i; ii = 0; for(i = i-1; i >= 0; --i){ /* last character of s place as first character of revline and so on */ revline[ii] = s[i]; ++ii; } revline[ii] = '\n'; revline[++ii] = '\0'; return 0; }
Solution by CakeOfTrust
A new way to implement the reverse function:
#include <stdio.h> #define EMPTY 1 #define NOTEMPTY 0 #define MAXLINE 1000 #define NO 1 #define YES 0 void getlines(char line[], int maxline, char ch[]); void reverse(char s[]); void getlines(char s[], int lim, char checker[]) { int c, i; for (i = 0; i < lim-1 && (c = getchar()) != EOF && c != '\n'; ++i) s[i] = c; if (c == '\n') { s[i] = c; ++i; } s[i] = '\0'; if (c == EOF) checker[0] = YES; if (i == 0) checker[1] = EMPTY; } void reverse(char s[]) { int i, len; char t; for (len = 0; s[len] != '\0'; ++len) ; for (i = len; i > len / 2.0 && (i - 1) != (len - i); --i) { t = s[i - 1]; s[i - 1] = s[len - i]; s[len - i] = t; } } int main(void) { int len = 0, max = len, templ = max; char line[MAXLINE], ch[2]; ch[0] = NO; ch[1] = NOTEMPTY; while (ch[0] == NO) { getlines(line, MAXLINE, ch); if (ch[1] == NOTEMPTY) { reverse(line); printf("%s", line); } } }
Solution by Luke Panayi
#include <stdio.h> #define MAXLINE 1000 void reverse(char str[], int lim); int c; /*defining c as a global variable so it's value can be checked in main*/ int main(){ char line[MAXLINE]; while (c != EOF){ reverse(line, MAXLINE); printf("%s", line); } return 0; } void reverse(char s[], int lim){ int i, len, j; for (i = 0; i<lim-1 && (c=getchar()) != EOF && c != '\n'; ++i){ s[i] = c; } if (c == '\n'){ s[i] = c; ++i; } /*just the getline function from the book*/ s[i] = '\0'; len = i; char n[len]; /*defines a constant length of the string (as i gets altered) and a temporary character array to store the reversed characters (directly using s causes problems)*/ for (j = 0; j <= len-2; --i){ if (s[i] != '\0' && s[i] != '\n'){ n[j] = s[i]; ++j; } } /*swaps around every non newline and null terminator character*/ n[j+1] = '\n'; n[j+2] = '\0'; /*adds on the missing characters to the end such that they don't get placed in the middle of the new strings*/ for (i = 0; n[i] != '\0'; ++i){ s[i] = n[i]; } /*copies the temporary character array back to the array given as an argument*/ }
Solution by Octavian
#include <stdio.h> #define MAXLEN 1000 int get_line_and_its_length(char current_line[], int len_current_line); void copy_reverse(char line[], char reverse_line[], int len_line); int main(void) { int len; char ln[MAXLEN]; char ln_reverse[MAXLEN]; len = 0; while ((len = get_line_and_its_length(ln, MAXLEN)) > 0){ copy_reverse(ln, ln_reverse, len); } printf("%s\n", ln_reverse); } int get_line_and_its_length(char _current_line[], int _len_current_line) { int i, c; for (i = 0; i < MAXLEN -1 && (c = getchar()) != EOF && c != '\n'; ++i) _current_line[i] = c; if (c == '\n') _current_line[i] = '\0'; return i; } void copy_reverse(char _line[], char _reverse_line[], int _len_line) { int i, j; j = 0; for (i = _len_line - 1; i >= 0 && i < MAXLEN - 1; --i){ _reverse_line[j] = _line[i]; ++j; } }
Solution by Pebl
#include <stdio.h> #define MAXLINE 1000 int get_l(char s[], int max); void reverse(char to[], char from[]); int main() { int len; int max; char line[MAXLINE]; char rev[MAXLINE]; max = 0; while ((len = get_l(line, MAXLINE)) > 0) if (len > max && len != '\n') { reverse(rev, line); len = '\n'; printf("\n %s", rev); } return 0; } int get_l(char s[], int lim) { int c, i; for (i = 0; i < lim - 1 && (c = getchar()) != EOF && c != '\n'; ++i) s[i] = c; if (c == '\n'){ s[i] = c; ++i; } s[i] = '\0'; return i; } void reverse(char to[], char from[]) { int i; int j, k; i = 0; while ((to[i] = from[i]) != '\0') for (i = 0; from[i] != '\0'; ++i) j = i -1; for (k = 0; k <= i; k++) { to[k] = from[j]; --j; } }