/*
 * rev_str.c
 * This program reads a sequence of strings, terminated by "." and reverses
 * each string.
 */

#include <stdio.h>
#include <string.h>

#define MAX_LENGTH 80

/* Prototypes: */
void reverse_str (char str[]);

int main ()
{
  char   str[MAX_LENGTH + 1];
  
  while (1)
  {
    /* Read the input string. */
    printf ("Please enter a string (or \".\" to stop): ");
    scanf ("%s", str);

    if (strcmp (str, ".") == 0)
      break;

    /* Reverse the string and print the result. */
    reverse_str (str);
    printf ("%s\n", str);
  }

  return (0);
}

/* ------------------------------------------------------------------------
 * Function: reverse_str
 * Purpose : Reverse a string.
 * In/Out  : str - A string (NULL terminated).
 *                 This variable will eventually contain the reversed string.
 * Returns : Nothing.
 */
void reverse_str (char str[])
{
  char   temp;
  int    i, j;

  /* Let i run from the first character forward, and let j run from the last
     character backward, until the two indices meet. */
  for (i = 0, j = strlen(str) - 1;
       i < j;
       i++, j--)
  {
    /* Swap the i'th and j'th characters. */
    temp = str[i];
    str[i] = str[j];
    str[j] = temp;
  }

  return;
}

