#include<stdio.h>
#define TABLEN 8
void detab(int n);
main()
{
int c;
int pos=0;
while((c = getchar()) != EOF ){
pos=pos%TABLEN;
if (c == '\t' && pos != 0){
detab(TABLEN-pos);
pos=0;
}else if(c=='\n'){
pos=0;
putchar(c);
}else{
++pos;
putchar(c);
}
}
}
void detab(int n)
{
while(n){
putchar('*');
n--;
}
}
# ./a.out
a a a
a*******a*******a
aa aa aa
aa******aa******aa
a bb ccc dddd eeeee ffffff ggggggg hhhhhhhh
a*******bb******ccc*****dddd****eeeee***ffffff**ggggggg*hhhhhhhh
在网上搜到 另一种实现:
#include <stdio.h>
#define TABINC 5 //tab increment size
//replace tabs with the proper number of blanks
int main(void)
{
int c, nb ,pos;
nb = 0; //number of blanks necessary
pos = 1; //position of character in line
while ((c = getchar()) != EOF)
{
if (c == '\t')
{
nb = TABINC - (pos - 1) % TABINC;
while (nb > 0)
{
putchar(' ');
pos++;
nb--;
}
}
else if (c == '\n')
{
putchar(c);
pos = 1;
}
else
{
putchar(c);
pos++;
}
}
return 0;
}