Write a program to remove trailing blanks and tabs from each line of input, and to delete entirely blank lines.

Source:

#include "stdio.h"
#include "wchar.h"

#define MAX_STRING_LENGTH 1024
#define MAX_STRINGS 64 

typedef signed char bool;
#define TRUE  1
#define FALSE 0

int copy_string(wchar_t string_a[MAX_STRING_LENGTH], wchar_t string_b[MAX_STRING_LENGTH])
{
  int i;

  for(i = 0; string_a[i] != L'\n'; i++)
  {
    string_b[i] = string_a[i];
  }
  /* make sure to end each string. */
  string_b[i] = L'\0';

  return 0;
}

int main(int argc, char *argv[])
{
  int i = 0;
  int o = 0;
  int string_index = 0;
  /* this strings are all printing incorrectly again so here's our string length array variable. */
  int string_length[MAX_STRINGS];

  wchar_t c;
  wchar_t input_string[MAX_STRING_LENGTH];
  wchar_t stored_string[MAX_STRINGS][MAX_STRING_LENGTH];

  bool running = TRUE;
  bool get_sentence = TRUE;


  printf("Please enter a few strings with a max length of %i.[ctrl]+[d] to exit.\n", MAX_STRING_LENGTH);
  printf("Press [enter] to submit string.\n");
  printf("I will remove the trailing blanks automatically (and lines that are only blanks).\n");
  printf("----------------------------------------------------------------------\n\n");

  while(running)
  {
    while(get_sentence)
    {
      c = getwchar();

      if(c == EOF)
      {
        get_sentence = FALSE;
        running = FALSE;
        break;
      }
      if(c == L'\n')
      {
        /* store our string. */
        copy_string(input_string, stored_string[string_index]);

        /* store our string length (for some reason) */
        string_length[string_index] = i;

        /* increment string index */
        string_index++;
        break;
      }
      if(string_index == MAX_STRINGS)
      {
        printf("Cannot store any more strings.\n");
        running = FALSE;
        get_sentence = FALSE;
        break;
      }

      input_string[i] = c;
      i++;
    }
    /* reset our counter. */
    i=0;

    /* get another sentence. */
    if(running)
    {
      get_sentence = TRUE;
    }
  }
  /* printf("----------------------------------------------------------------------\n"); */

  /* Let's check our strings for trailing spaces
   */
  /* have we reached the end of the sentence? */
  bool sentence_end = FALSE;
  
  /* this stores a value for each sentence. Non-whitespace characters add value.
   * If the sentence's sum is 0 then we should not print it. 
   */
  int sentence_value[MAX_STRINGS] = {0};

  /* here's our loop to sort through the sentences BACKWARDS. */
  for(o=0; o < string_index; o++)
  {
    /* notice the inverse here vvv */ /* and that tricky -1 that makes us not print bad things... :( */
    for(i=string_length[o]-1; i >= 0; i--)
    {
      /* printf("%c='%i'\n",stored_string[o][i], stored_string[o][i]); */

      if(stored_string[o][i] != 0 && stored_string[o][i] != 32 && stored_string[o][i] != 9) /* 0 == '\0', 32 == space, 9 == tab */
      {
        /* printf("This is not a null or a space! Setting sentence_end TRUE!\n"); */
        sentence_end = TRUE;

        /* increase the sentence value. */
        sentence_value[o] += 1;
      }

      if((stored_string[o][i] == 32 || stored_string[o][i] == 9) && !sentence_end)
      {
        /* change to this if you want to ACTUALLY remove the trailing white space.
        stored_string[o][i] = L'\0';
        */
        stored_string[o][i] = L'x';
      }
    }
    /* printf("sentence_value[o]=%i\n", sentence_value[o]); */
    sentence_end = FALSE;
  }
      

  /* print it.
   *
   * note: I could not get wprintf to work at all so I'm just using a loop.
   *
   */
  printf("\n----------------------------------------------------------------------\n\n");
  for(o=0; o < string_index; o++)
  {
    /* start by checking the sentence value, if there is none increment zero to not print it. 
     */
    /* printf("sentence_value[o]=%i\n", sentence_value[o]); */
    if(sentence_value[o] != 0)
    {
      /* printf("string_length[o]=%i\n", string_length[o]);*/
      for(i=0; i < string_length[o]; i++)
      {
        printf("%c", stored_string[o][i]);
      }
      printf("\n", o);
    }
  }
  printf("\n----------------------------------------------------------------------\n");

  return 0;
}

Output:

Please enter a few strings with a max length of 1024.[ctrl]+[d] to exit.
Press [enter] to submit string.
I will remove the trailing blanks automatically (and lines that are only blanks).
----------------------------------------------------------------------

This is just a test.

No trailing space.

----------------------------------------------------------------------

This is just a test.xxx
No trailing space.

----------------------------------------------------------------------