From a264cc5dd33bcec712d946704f3c53d17deb6b83 Mon Sep 17 00:00:00 2001 From: Sadeep Madurange Date: Sun, 21 Nov 2021 15:54:43 +0800 Subject: 1.22 --- 1/22.c | 53 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 53 insertions(+) create mode 100644 1/22.c (limited to '1') diff --git a/1/22.c b/1/22.c new file mode 100644 index 0000000..39dbd6c --- /dev/null +++ b/1/22.c @@ -0,0 +1,53 @@ +#include +#include + +#define MAXLEN 1000 + +void getstr(char s[]); +void fold(char to[], char from[], int n); + +/* folds long input */ +int main(int argc, char *argv[]) { + char s[MAXLEN], f[MAXLEN]; + + printf("Enter some text and press ctrl+d to wrap text\n"); + + getstr(s); + fold(f, s, atoi(argv[1])); + printf("\n%s\n", f); + + return 0; +} + +void getstr(char s[]) { + int i, c; + + for (i = 0; i < MAXLEN - 1 && (c = getchar()) != EOF; i++) + s[i] = c; + s[i] = '\0'; +} + +void fold(char to[], char from[], int n) { + int i, j, len, blank; + char c; + + len = 0; + + for (i = 0, j = 0; j < MAXLEN - 1 && (c = from[i]) != '\0'; i++) { + blank = c == ' ' || c == '\t'; + if (len == 0 && blank) + continue; + + if (len >= n) { + if (!blank) { + to[j++] = '\n'; + len = 0; + } + } + + to[j++] = c; + len++; + } + + to[j] = '\0'; +} \ No newline at end of file -- cgit v1.2.3