将输入中的制表符替换成适当数目的空格,使空格充满到下一个制表符终止的地方

来源:互联网 发布:ubuntu apt get glib 编辑:程序博客网 时间:2024/05/01 22:48
#include <stdio.h>#define NSPACE  8int main(int argc, char *argv[]){    int c;    int npos=1;    while((c=getchar())!=EOF)    {        if(c=='\t')        {            int nb=NSPACE-(npos-1)%NSPACE;//列举:当字符在第一位时,需打印8个空格,第二位,需打印7个空格,类推:NSPACE-(npos%NSPACE)+1            while(nb>0)            {                putchar(' ');                npos++;                nb--;            }        }        else if(c=='\n')        {            putchar(c);            npos=1;        }        else        {            putchar(c);            npos++;        }    }    return 0;}


原创粉丝点击