summaryrefslogtreecommitdiffstats
path: root/main.c
blob: 282da8fd6255b5dba25d642d525f2af8b1a17ebc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
#include <stdio.h>

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;
}