#include int main(void) { char c; unsigned short numBlanks = 0;//number of blanks stored unsigned short i;//loop int while ((c=getchar()) != EOF)//keep on accepting input characters until EOF is given { if (c == ' '){++numBlanks;}//if character is space, number of blanks+=1 if (c == ' '){numBlanks += 4;}//if character is tab, number of blanks+=4 if ((c != ' ') && (c != ' ') && (numBlanks > 0))//if there were previously recorded blanks, but current is a non-blank character, print the optimised amount of blanks { if ((numBlanks % 4) == 0)//if the number of spaces are evenly divisable by 4, only print tabs. { for (i=0;i<(numBlanks/4);++i){putchar(' ');}//place tab for every 4 spaces numBlanks = 0;//we are done printing all blanks } else //otherwise, go into tab/space mode { while (numBlanks != 0)//keep on printing until there are no more blanks to print { if (numBlanks > 3){putchar(' '); numBlanks -= 4;}//if the number of blanks is 4 or above, print a tab and reduce the number of blanks else {putchar(' '); --numBlanks;}//otherwise, print a single blank } } } if (numBlanks == 0)//if character is non-blank, or all blanks have just been printed, print the character { putchar(c); } } return 0; }