diff options
| author | Sadeep Madurange <smadurange@users.noreply.github.com> | 2021-11-20 21:22:22 +0800 |
|---|---|---|
| committer | Sadeep Madurange <smadurange@users.noreply.github.com> | 2021-11-20 21:22:22 +0800 |
| commit | 6a047d173de71046d9e10d07fcbd39bbaf9078ed (patch) | |
| tree | 8590e2f2cf935613f568342386bfad3858d5554f /1/17.c | |
| parent | e8064efb940701215fb9c0576c997a13e7b67feb (diff) | |
| download | k&r-exercises-6a047d173de71046d9e10d07fcbd39bbaf9078ed.tar.gz | |
1.17:
Diffstat (limited to '1/17.c')
| -rw-r--r-- | 1/17.c | 34 |
1 files changed, 34 insertions, 0 deletions
@@ -0,0 +1,34 @@ +#include <stdio.h> + +#define MAXLEN 1001 +#define PRNLEN 80 + +int cgetline(char s[], int max); + +/* prints inputs longer than 80 characters */ +int main(int argc, char *argv[]) { + int size; + char s[MAXLEN]; + + while ((size = cgetline(s, MAXLEN)) > 0) { + if (size >= PRNLEN) + printf("%s", s); + } + + if (size == -1) { + printf("exceeded max input size: %d\n", MAXLEN - 1); + return 1; + } + + return 0; +} + +int cgetline(char s[], int max) { + int c, i; + + for (i = 0; i < max - 1 && (c = getchar()) != EOF; i++) + s[i] = c; + s[i] = '\0'; + + return c != '\n' && c != EOF ? -1 : i; +}
\ No newline at end of file |
