Task (1.20)
Write a program detab that replaces tabs in the input with the proper number of blanks to space to the next tab stop.
Also, the condition is is not to use pointers and structs etc because they will be considered in next chapters.
I'm new in C, and want to know what in my solution is bad idea and what can be done other way (but without pointers):
#include <stdio.h>
#define TABLINE 4
int count(char stri[]);
void main() {
printf ("\nTabline is %d\n", TABLINE);
int i = 0;
int h;
char stri[100];
char c;
while ((c = getchar()) != '\n' && c != EOF) {
if (c == '\t')
for (h=0; h<TABLINE; ++h) {
stri[i] = ' ';
++i;
} else {
stri[i] = c;
++i;
}
}
printf ("%d\n", count(stri));
for (i=0; i< count(stri); ++i)
printf ("%c", stri[i]);
}
int count(char stri[]) {
int l;
for (l=0; stri[l]; ++l)
;
return l;
}
And its work:
$ ./detab Tabline is 4 befortan aftertab 20 befortan aftertab