/*

 * rep_alpha.c

 * This program reads a string and replaces all the alphabetic characters

 * in it, according to a given replacement table.

 */

 

#include <stdio.h>

#include <ctype.h>

 

#define MAX_LEN 200

 

int main ()

{

  char   rep_tab[] = "QWERTYUIOPASDFGHJKLZXCVBNM";

                                /* The replacement table: The i'th character

                                 * in the Latin alphabet should be replaced

                                 * by the i'th character of this string. */

 

  char   str_in [MAX_LEN + 1];  /* The input string. */

  int    l_str;                 /* Its length. */

  char   str_out [MAX_LEN + 1]; /* The output string. */

  int    c;

  int    i;

 

  /* Read the input string, character by character. */

  printf ("Please enter the input string: ");

 

  l_str = 0;

  c = getchar();

  while (c != '\n' && c != EOF)

  {

    /* Watch out for inputs that are too long! */

    if (l_str > MAX_LEN)

    {

      printf ("Error -- input string too long.\n");

      return (1);

    }

 

    /* Store the current character. */

    str_in[l_str] = c;

    l_str++;

 

    /* Read the next input character. */

    c = getchar();

  }

 

  /* Take care of the terminating NULL. */

  str_in[l_str] = '\0';

 

  /* Go over the input characters. */

  for (i = 0; i < l_str; i++)

  {

    /* Check if the current character is a letter. */

    if (isalpha (str_in[i]))

    {

      /* Convert to an upper-case character and use the replacement table to

       * obtain the target character. */

      c = toupper (str_in[i]);

      str_out[i] = rep_tab[c - 'A'];

    }

    else

    {

      /* Not a letter: Leave the character as is. */

      str_out[i] = str_in[i];

    }

  }

 

  /* Take care of the terminating NULL. */

  str_out[l_str] = '\0';

 

  /* Print the two string. */

  printf ("Input : %s\n", str_in);

  printf ("Output: %s\n", str_out);

 

  return (0);

}