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?
(2)空格位置经过了8的倍数,所以位置7、8被制表符占据,剩余三个空格依然是空格
      
#include<stdio.h>
#define MAXLEN 1024
#define TAB 8
int getlines(int array[] , int maxlen);
int getlines(int array[] , int maxlen)//将输入读入数组,并且在换行时输出结果
{
    int i,c;
    for ( i = 0; i < maxlen-1 && (c=getchar())!=EOF && c!=‘\n‘; i++)
    {
        array[i] = c;
    }
    if (c==‘\n‘)
    {
        array[i] = c;
        i++;
    }
    array[i] = ‘\0‘;
    return i;
}
int main()
{
    int array[MAXLEN];
    int len;
    int i;
    int nb=0;//空格计数器
    int nt=0;//制表符计数器
    while ((len = getlines(array , MAXLEN)) > 0)
    {
        for ( i = 0; i < len ; i++)
        {
            if (array[i] == ‘ ‘)
            {
                //统计同一行两段连续字符中间需要输出多少制表符和空格
                if ((i+1)%TAB != 0)//i从零开始,但字符位置从1开始,(i+1)表示当前位置
                {
                    nb++;
                }else
                {
                    nt++;
                    nb = 0;//制表符永远在空格前面,有了制表符,空格就要清零
                }
            }else
            {
                for ( ; nt > 0; nt--)
                {
                    putchar(‘\t‘);
                }
                if (array[i] == ‘\t‘)
                {
                    nb = 0;
                }else
                {
                    for ( ; nb > 0; nb--)
                    {
                        putchar(‘*‘);
                    }
                }
                putchar(array[i]);
            }
        }
    }
    
    return 0;
}
 
原文:https://www.cnblogs.com/jerryleesir/p/12822876.html