From de32013d6c59126470c41f7eb57d28ad460b0127 Mon Sep 17 00:00:00 2001 From: NaiJi Date: Thu, 2 May 2024 15:15:30 +0400 Subject: [PATCH] 1.21 --- The C Programming Language/1.21.c | 59 +++++++++++++++++++++++++++++++ 1 file changed, 59 insertions(+) create mode 100644 The C Programming Language/1.21.c diff --git a/The C Programming Language/1.21.c b/The C Programming Language/1.21.c new file mode 100644 index 0000000..c6dd7f2 --- /dev/null +++ b/The C Programming Language/1.21.c @@ -0,0 +1,59 @@ +#include + + +int _getline(char s[], int lim) +{ + int c, i, j; + for (i = 0; (c = getchar()) != '\n'; ++i) + { + if (i < lim - 1) + s[i] = c; + } + + j = i; + if (j > lim - 1) + j = lim - 1; + s[j] = '\0'; + return j - 1; +} + +void entab(char to[], char from[], int columns) +{ + int spaces_counter = 0; + int from_i = 0; + int to_i = 0; + + while (from[from_i] != '\0') + { + ++spaces_counter; + ++from_i; + if (spaces_counter == columns) + { + to[to_i] = '\t'; + ++to_i; + spaces_counter = 0; + } + } + + while (spaces_counter > 0) + { + to[to_i] = '_'; + ++to_i; + --spaces_counter; + } + + to[to_i] = '\0'; +} + +main() +{ + int size = 500; + char from[size]; + char to[size]; + while (1) + { + _getline(from, size); + entab(to, from, 4); + printf("%s\n", to); + } +}