Write a program that takes input and replaces multiple spaces with a single space.

Source:

#include "stdio.h"

#define TRUE 1
#define FALSE 0

int main(int argc, char *argv[])
{
  int running = TRUE;

  int character;
  int last_character_was_space = FALSE;

  int whitespace_duplicate_counter = 0;
  int redundant_spaces = 0;

  printf("Enter a bunch of text with extra whitespaces and I will track and remove them!.\n[ctrl]+[d] to quit.\n");

  while(running)
  {
    character = getchar();

    if(character != '\n' && character != EOF && character != ' ')
    {
      printf("%c", character);
      last_character_was_space = FALSE;
    }
    else if(character == ' ')
    {
      if(last_character_was_space)
      {
        printf("_");
        redundant_spaces += 1;
      }
      else
      {
        last_character_was_space = TRUE;
        printf(" ");
      }
    }
    else if(character == '\n')
    {
      printf("\n%i redundant whitespace detected.\n", redundant_spaces);
    }
    else if(character == EOF)
    {
      printf("\n[EOF] entered, goodbye!\n");
      running = 0;
    }
  }

  return 0;
}

Output:

Enter a bunch of text with extra whitespaces and I will track and remove them!.
[ctrl]+[d] to quit.
This is  just a  test.
This is _just a _test.
2 redundant whitespace detected.

[EOF] entered, goodbye!