/*
 * Illustrate the uses of getchar and putchar.
 * Program prompts the user to type two lines of text
 *         the first line is read character-by-character
 *             and immediately echoed to the terminal
 *         the second line is read character-by-character into an array
 *             a null character is added at the end of input
 *             after all reading is done, the array is printed as a string
 */

#include <stdio.h>

int
main()
{
  char a;
  const int linesz = 80;

  printf ("Enter a line of text: ");

  /* read character by character until new line (\n) is read */
  a = getchar();
  
  printf("line read:  ");
  while (a != '\n')
    {
      putchar(a);                                  /* echo previous character */
      a = getchar();                                    /* get next character */
    }
  putchar('\n');                /* reading of line done, so move to next line */

  char line[linesz+1];               /* buffer of linesz  plus null character */
  int index = 0;

  printf("Enter a second line of text (no more than %d characters): ",linesz);
  
  /* loop guard combines reading, placing in array and newline check */
  while ((line[index] = getchar()) != '\n')
    {
      index++;
    }
  line[index] = 0;  /* newline char at end--replace by null for end of string */

  printf ("second line read:  %s\n", line);         /* print line as a string */

  return 0;
}
