diff options
| -rw-r--r-- | 1/17.c | 2 | ||||
| -rw-r--r-- | 1/19.c | 46 |
2 files changed, 47 insertions, 1 deletions
@@ -30,5 +30,5 @@ int cgetline(char s[], int max) { s[i] = c; s[i] = '\0'; - return c != '\n' && c != EOF ? -1 : i; + return c != '\n' && c != EOF ? -1 : i - 1; }
\ No newline at end of file @@ -0,0 +1,46 @@ +#include <stdio.h> + +#define MAXLEN 5 + +int cgetline(char s[], int max); +void reverse(char s[], int size); + +/* reverses input string */ +int main(int argc, char *argv[]) { + int size; + char s[MAXLEN]; + + while ((size = cgetline(s, MAXLEN)) > 0) { + reverse(s, size); + printf("%s\n", s); + } + + if (size == -1) { + printf("exceeded max input size: %d\n", MAXLEN); + return 1; + } + + return 0; +} + +int cgetline(char s[], int max) { + int c, i; + + for (i = 0; i < max - 1 && (c = getchar()) != EOF && c != '\n'; i++) + s[i] = c; + s[i] = '\0'; + + return i == max && c != '\n' && c != EOF ? -1 : i; +} + +void reverse(char s[], int size) { + int i, j, lim; + + lim = size / 2; + + for (i = 0, j = size - i - 1; i < lim; i++) { + s[i] = s[i] ^ s[j]; + s[j] = s[i] ^ s[j]; + s[i] = s[i] ^ s[j]; + } +}
\ No newline at end of file |
