From f72ba11006b4b4496709d38fea0ac4854d5ba30f Mon Sep 17 00:00:00 2001 From: Sadeep Madurange Date: Sun, 21 Nov 2021 13:06:29 +0800 Subject: 1.21 --- 1/20.c | 7 ++++--- 1/21.c | 59 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 63 insertions(+), 3 deletions(-) create mode 100644 1/21.c (limited to '1') diff --git a/1/20.c b/1/20.c index a4f38cb..7ec32ac 100644 --- a/1/20.c +++ b/1/20.c @@ -8,14 +8,15 @@ void detab(char to[], char from[], int n); /* detabs input by replacing tabs with specified number of blanks */ int main(int argc, char *argv[]) { + int tabw; char s[MAXLEN], dtab[MAXLEN]; - int tabw = atoi(argv[1]); - printf("Enter text to detab and press ctrl+d\n"); + tabw = atoi(argv[1]); + printf("Enter text to detab and press ctrl+d\n"); + getstr(s); detab(dtab, s, tabw); - printf("\nTab width: %d\n", tabw); printf("%s\n", dtab); diff --git a/1/21.c b/1/21.c new file mode 100644 index 0000000..8648ee2 --- /dev/null +++ b/1/21.c @@ -0,0 +1,59 @@ +#include +#include + +#define MAXLEN 1000 + +void getstr(char s[]); +void entab(char to[], char from[], int n); + +/* replaces specified number of blanks with a tabstop */ +int main(int argc, char *argv[]) { + int tabw; + char s[MAXLEN], tabbed[MAXLEN]; + + tabw = atoi(argv[1]); + + printf("Enter text to entab and press ctrl+d\n"); + + getstr(s); + entab(tabbed, s, tabw); + printf("\nTab width: %d\n", tabw); + printf("%s\n", tabbed); + + 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 entab(char to[], char from[], int n) { + int i, j, k, op; + char c; + + for (i = 0, j = 0; j < MAXLEN - 1 && (c = from[j]) != '\0'; i++) { + if (c == ' ') { + op = j + n < MAXLEN - 1; + for (k = j + 1; op && k < j + n - 1; k++) { + if (from[k] != ' ') { + op = 0; + } + } + + if (op) { + to[i] = '\t'; + j += n; + continue; + } + } + + to[i] = c; + j++; + } + + to[i] = '\0'; +} \ No newline at end of file -- cgit v1.2.3