/*

 * lat2dec.c

 * This program reads an "alphabetic number" (in base-26), converts it

 * to a numerical value and prints it as a decimal number.

 */

 

#include <stdio.h>

 

int main ()

{

  const int     base = 26;      /* Our alphabetic base has 26 "digits":

                                   {A, B, ... , Z}. */

  int           c;              /* The current "digit" (character). */

  unsigned int  n = 0;          /* The current numerical value. */

 

  printf ("Please enter an alphabetic number: ");

 

  /* Read characters until reaching the end of the line. */

  while ((c = getchar()) != '\n')

  {

    /* Make sure that the character represents an alphabetic "digit". */

    if (! (c >= 'A' && c <= 'Z'))

    {

      /* Print an error message and exit. */

      printf ("\n Illegal digit: %c -- Aborting.\n", c);

      return (1);

    }

 

    /* The digit c is in the range 'A' -- 'Z', so 0 <= (c - 'A') <= 25.

     * We multiply the current value by the base, and add to it (c - 'A'). */

    n = n*base + (c - 'A');

  }

 

  /* Print the result. */

  printf ("The decimal value is %d.\n", n);

 

  return (0);

}