Write a program entab that replaces strings of blanks with the minimum number of tabs and blanks to achieve the same spacing. Use the same stops as for detab . When either a tab or a single blank would suffice to reach a tab stop, which should be given preference?
#include<stdio.h>
#define TABINC 8
#define MAXLINE 1000
void entab(char line[]);
int Getline(char s[], int lim);
main()
{
char line[MAXLINE];
int len;
while((len = Getline(line, MAXLINE)) > 0){
entab(line);
}
}
int Getline(char s[], int lim)
{
int i, c;
for(i = 0; ((c = getchar()) != EOF) && c != ‘\n’; ++i)
s[i] = c;
if(c == ‘\n’){
s[i] = ‘\n’;
++i;
}
s[i] = ‘\0’;
return i;
}
void entab(char s[])
{
int i, nt, nb;
nt = nb = 0;
for(i = 0; s[i] != ‘\n’; ++i){
if(s[i] == ’ ‘){
if((i + 1) % TABINC != 0) {//Not enough blanks for a tab
++nb;
} else {
nb = 0;
++nt; //Now has TABINC blanks to replace with a tab
}
} else {
for(; nt > 0; —nt)
putchar(‘\t’);
if(s[i] == ‘\t’){
nb = 0;
putchar(‘\t’);
}
else
for(; nb > 0; —nb)
putchar(’ ‘);
printf(“%c”, s[i]);
}
}
putchar(‘\n’);
}