summarylogtreecommitdiffstats
path: root/main.c
blob: 3eb15d4fb61893c74aeb458b25ec8254101f7cea (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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>

void step(char last, char curr)
{
  char tmp;

  if(curr == '\n')
  {
    /* check if the next char is a line spaces as well,
       if so don't do a damn thing! */

    tmp = getchar();

    if(tmp == EOF)
    {
      /* kind of whacky, lets just output the newline and exit I suppose. */
      putchar(curr);
      putchar(tmp);      
      return;
    }
    else if (tmp == '\n')
    {
      /* okay cool, just leave alone! */
      /* can prob combine w/ func above */
      putchar(curr);
      putchar(tmp);
      return;
    }
    else 
    {
      /* business as normal, here we don't want to put out the newline but we 
         also really don't want to put out two whitespace characters right
         next to each other. Reverse and make sure we're not! */

      /* set last, then check if last and tmp are both spaces, if so only
         write one! */

      /* Well, fuck. We cannot seek on standard input */

      if(last == ' ' && tmp == ' ')
      {
        /* no op */
      }
      else if(last != ' ' && tmp != ' ')
      {
        putchar(' ');
        putchar(tmp);
      }
      else
      {
        putchar(tmp);
      }
    }
  }
  else
  {
    putchar(curr);
  }
}

int main(void) 
{
  char last, curr;

  last = '\0';
  curr = getchar();

  while(curr != EOF)
  {
    step(last, curr);

    last = curr;
    curr = getchar();
  }

  return 0;
}