Write a program to print all input lines that are longer than 80 characters.

Source:

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

#define MAX_STRING_LENGTH 1024
/* max number of arbitrarily long strings to store. */
#define MAX_LONG_STRINGS 64
/* what is arbitrarily long? */
#define ARBITRARILY_LONG  80

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] = '\n';

  return 0;
}

int main(int argc, char *argv[])
{
  int i = 0;
  int o = 0;

  wchar_t c;
  wchar_t input_string[MAX_STRING_LENGTH];
  wchar_t stored_string[MAX_STRING_LENGTH];
  /*
  int old_i = 0;
  wchar_t longest_string[MAX_STRING_LENGTH];
  */
  wchar_t arbitrarily_long_strings[MAX_LONG_STRINGS][MAX_STRING_LENGTH] = {'\n'};
  int a_position = 0; /* out arbitrarily long string storage index. */
  /* I couldn't get the loop to print the long strings correctly so I am
   * implementing an array for storing the length of each string. ffs
   */
  int arbitrarily_long_strings_length[MAX_LONG_STRINGS] = {0};

  bool running = TRUE;
  bool get_sentence = TRUE;


  printf("Please enter a string with a max length of %i.\n[ctrl]+[d] to exit.\n", MAX_STRING_LENGTH);
  printf("----------------------------------------------------------------------\n");

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

      if(c == EOF)
      {
        get_sentence = FALSE;
        running = FALSE;
        break;
      }
      if(c == L'\n')
      {
        get_sentence = FALSE;
        /* now that we got a new sentence we should check 
         * to see if it is longer than our longest. 
         * we start by storing the i value, which is a cheap way
         * to get our sentence length. 
         *
        if( i > old_i )
        {
          old_i = i;
          * and we copy the sentence into our longest string variable. 
           *
          copy_string(input_string, longest_string);
        }
        */
        /* now the assignment is to display an arbitrarily long strings. 
         * so we store in a string array any strings longer than 
         * ARBITRARILY_LONG characters. 
         */
        if(i > ARBITRARILY_LONG)
        {
          /* deal with if they have too many long strings. */
          if(a_position > MAX_LONG_STRINGS)
          {
            /* just loop it... */
            a_position = 0;
          }
          /* store the length. */
          arbitrarily_long_strings_length[a_position] = i;
          /* store string */
          copy_string(input_string, arbitrarily_long_strings[a_position]);
          /* increment our position in the array. */
          a_position++;
        }
        break;
      }

      input_string[i] = c;
      i++;
    }
    /* reset our counter. */
    i=0;
    /* store our string. */
    //copy_string(input_string, stored_string);
    //printf("[sentence stored.]\n");

    /* Get another sentence. */
    if(running)
    {
      get_sentence = TRUE;
    }
  }

  /* print it.
   *
   * note: I could not get wprintf to work at all so I'm just using a loop.
   *
   *
  printf("\nThe longest string entered:\n"
    "--------------------------------------------------------------------\n");
  for(i=0; i < old_i; i++)
  {
    printf("%lc", longest_string[i]); 
  }
  printf("\nString length: %i\n", old_i);
  */

  printf("\nStrings over 80 characters long:\n"
    "--------------------------------------------------------------------\n");
  if(a_position > 0)  /* if the array has been used at all then print the array. */
  {
    /* I found by trial and error that I need to go back one position in the array
     * by subtracting 1 to a_position otherwise it tries to print an array that 
     * doesn't exist. :o
     */
    a_position-=1;

    /* loop through the array to print the long strings. */
    for(; a_position >= 0; a_position--)
    {
      for(i=0; < arbitrarily_long_strings_length[a_position]; i++)
      {
        printf("%lc", arbitrarily_long_strings[a_position][i]);
      }
      printf("\nString length: %i\n", arbitrarily_long_strings_length[a_position]);
    }
  }
  else
  {
    printf("No arbitrarily long sentences.\n");
  }

  return 0;
}

Output:

Please enter a string with a max length of 1024.
[ctrl]+[d] to exit.
----------------------------------------------------------------------
This is just a test.
This is a long string to hopefully trip the detection of a sentence that is over characters long. Let's all hope
this works! Strings over 80 characters long: -------------------------------------------------------------------- This is a long string to hopefully trip the detection of a sentence that is over characters long. Let's all hope
this works! String length: 124