summaryrefslogtreecommitdiffstats
path: root/main.c
diff options
context:
space:
mode:
authorGentoo <installgentoo@endianness.com>2021-03-27 10:13:53 +1100
committerGentoo <installgentoo@endianness.com>2021-03-27 10:13:53 +1100
commit9b251701b75ad04e66d33e1d3a7d39b4062a9ea5 (patch)
tree5c22ec8fb84ce5e95d8d280bb2676e75955c9fc2 /main.c
downloadoptimise-space-chars-9b251701b75ad04e66d33e1d3a7d39b4062a9ea5.tar.gz
optimise-space-chars-9b251701b75ad04e66d33e1d3a7d39b4062a9ea5.tar.bz2
optimise-space-chars-9b251701b75ad04e66d33e1d3a7d39b4062a9ea5.zip
initial commitHEADmaster
Diffstat (limited to 'main.c')
-rw-r--r--main.c41
1 files changed, 41 insertions, 0 deletions
diff --git a/main.c b/main.c
new file mode 100644
index 0000000..282da8f
--- /dev/null
+++ b/main.c
@@ -0,0 +1,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;
+}