diff options
| author | Sadeep Madurange <smadurange@users.noreply.github.com> | 2021-11-21 12:20:24 +0800 |
|---|---|---|
| committer | Sadeep Madurange <smadurange@users.noreply.github.com> | 2021-11-21 12:20:24 +0800 |
| commit | ea74e68369f44bf5766704db626c23b001f5fdc3 (patch) | |
| tree | 116f4624605cb84bf29f374684cf87d58e1fc83d | |
| parent | 6f4c5b043451ebb574c35e0d7a2a77c000cf1a8e (diff) | |
| download | k&r-exercises-ea74e68369f44bf5766704db626c23b001f5fdc3.tar.gz | |
1.20
| -rw-r--r-- | 1/20.c | 47 |
1 files changed, 47 insertions, 0 deletions
@@ -0,0 +1,47 @@ +#include <stdio.h> +#include <stdlib.h> + +#define MAXLEN 1000 + +void getstr(char s[]); +void detab(char to[], char from[], int n); + +/* detabs input by replacing tabs with specified number of blanks */ +int main(int argc, char *argv[]) { + char s[MAXLEN], dtab[MAXLEN]; + int 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); + + 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 detab(char to[], char from[], int n) { + int i, j, k; + char c; + + for (i = 0, j = 0; j < MAXLEN - 1 && (c = from[i]) != '\0'; i++) { + if (c == '\t') { + for (k = 0; k < n; k++) + to[j++] = ' '; + } else { + to[j++] = from[i]; + } + } + + to[j] = '\0'; +}
\ No newline at end of file |
