Write a function "reverse()" that reverses the character string "s". Use it to write a program that reverses its input a line at a time.

Source:

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

#define MAX_STRING_LENGTH 1024

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

int reverse(wchar_t input[MAX_STRING_LENGTH], int input_length, wchar_t output[MAX_STRING_LENGTH])
{
  int i; 
  int o = 0;

  for(i = input_length; i >= 0; i--)
  {
    /* printf("copying letter %c\n", input[i]); */
    output[o] = input[i];
    o++;
  }
  /* make sure to end each string. */
  output[o] = L'\0';

  return 0;
}

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

  wchar_t c;
  wchar_t s[MAX_STRING_LENGTH];
  wchar_t stored_string[MAX_STRING_LENGTH];

  bool running = TRUE;
  bool get_sentence = TRUE;


  printf("Please enter a string with a max length of %i.[ctrl]+[d] to exit.\n", MAX_STRING_LENGTH);
  printf("Press [enter] to submit string.\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 length (for some reason) */
        string_length = i-1;

        /* reverse our string. */
        reverse(s, string_length, stored_string);
        /* print it.
         *
         * note: I could not get wprintf to work at all so I'm just using a loop.
         *
         */
        printf("\n----------------------------------------------------------------------\n\n");
        
        /* printf("string_length=%i\n", string_length[o]);*/
        for(o=0; o <= string_length; o++)
        {
          printf("%c", stored_string[o]);
        }
        printf("\n\n----------------------------------------------------------------------\n\n");

        break;
      }

      s[i] = c;
      i++;
    }

    /* reset our counter. */
    i=0;

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



  return 0;
}

Output:

Please enter a string with a max length of 1024.[ctrl]+[d] to exit.
Press [enter] to submit string.
----------------------------------------------------------------------

This is just a test.

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

.tset a tsuj si sihT

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

I hope you find this interesting.

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

.gnitseretni siht dnif uoy epoh I

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