summaryrefslogtreecommitdiffstats
path: root/1/21.c
diff options
context:
space:
mode:
Diffstat (limited to '1/21.c')
-rw-r--r--1/21.c59
1 files changed, 59 insertions, 0 deletions
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 <stdio.h>
+#include <stdlib.h>
+
+#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