#include #define MAXLINE 1000 // maximum input line length #define TABSIZE 8 // tab size int getline(char* line, int maxline); void detab(char* line, int tabsize); // replaces tabs in the input with the proper number of spaces // to the next tab stop int main() { int len; // current line length char line[MAXLINE]; // current input line while ((len = getline(line, MAXLINE)) > 0) { if (len > 0) detab(line, TABSIZE); printf("%s", line); } return 0; } // getline: read a line into s, return length int getline(char* s, int maxline) { int c, i, j; for (i = 0, j = 0; (c = getchar()) != EOF && c != '\n'; ++i) if (j < maxline - 2) // 2 bytes reserved for '\n' and '\0' s[j++] = c; if (c == '\n') { s[j++] = c; ++i; } s[j] = '\0'; return i; } // detab: replaces tabs in the input with the proper number of spaces // to the next tab stop void detab(char* s, int tabsize) { int i = 0; // advance from s[0] int j = 0; int k; while (s[j]) ++j; // points to '\0' while (s[i]) { if (s[i] == '\t') { int insert = tabsize - i % tabsize - 1; for (k = j; k > i; --k) s[k + insert] = s[k]; for (k = 0; k <= insert; ++k) s[i + k] = ' '; i += insert + 1; j += insert; } else ++i; } }