/* Filename: HW6.c */ /* This program encodes and decodes messages*/ /****************************************************************** * * * Programmer: Matthew Glynn * * Date : 03/08/2007 * * Section Number: B36 * * Filename: HW6.c * * Description: This program encodes and decodes messages * * * ******************************************************************/ #include /* library to be used */ /* Declare functions */ void printMenu( void ); void encode( void ); void decode( void ); /* Declare Global variables */ char alphabet[26] = {'a'/*1*/, 'b'/*2*/, 'c'/*3*/, 'd'/*4*/, 'e'/*5*/, 'f'/*6*/, 'g'/*7*/, 'h'/*8*/, 'i'/*9*/, 'j'/*10*/, 'k'/*11*/, 'l'/*12*/, 'm'/*13*/, 'n'/*14*/, 'o'/*15*/, 'p'/*16*/, 'q'/*17*/, 'r'/*18*/, 's'/*19*/, 't'/*20*/, 'u'/*21*/, 'v'/*22*/, 'w'/*23*/, 'x'/*24*/, 'y'/*25*/, 'z'/*26*/} ; char cipher[26] = {'m'/*1*/, 'a'/*2*/, 't'/*3*/, 'h'/*4*/, 'e'/*5*/, 'w'/*6*/, 'g'/*7*/, 'l'/*8*/, 'y'/*9*/, 'n'/*10*/, 'r'/*11*/, 'f'/*12*/, 's'/*13*/, 'd'/*14*/, 'k'/*15*/, 'i'/*16*/, 'j'/*17*/, 'u'/*18*/, 'v'/*19*/, 'p'/*20*/, 'x'/*21*/, 'q'/*22*/, 'z'/*23*/, 'o'/*24*/, 'b'/*25*/, 'c'/*26*/} ; char message[500]; char junk;/* for the /n */ int counter, counter2, length, choice; char *alphabetPtr = alphabet; char *cipherPtr = cipher; char *messagePtr = message; int main( void ) {/* start main */ do {/* start do */ printMenu();/* CALL FUNCTION PRINTMENU*/ switch ( choice )/* Switch on menu choice entered by user */ { case 1: encode();/* CALL FUNCTION encode*/ break; case 2: decode();/* CALL FUNCTION decode*/ break; case 3: break; default: /* display default when a choice <0 or >4 has been entered */ printf(" An invalid entry was made.\n"); break; } } while (choice !=3); /* when an entry of 3 is made, exit program */ return 0; /* indicate program ended successfully */ }/* End funcyion main */ /****************************************************************** * * * Programmer: Matthew Glynn * * Name: printMenu * * Function: This function prints the main menu of the program. * * Algorithm: This function is a series of printf statements. * * Input: none * * Output: changes the global variable choice, so no real output * * * ******************************************************************/ void printMenu( void ) { printf("\n 1. Encode a message\n"); printf(" 2. Decode a message\n"); printf(" 3. Exit the program\n"); scanf("%d", &choice); printf("\n"); } /****************************************************************** * * * Programmer: Matthew Glynn * * Name: encode * * Function: Encodes the message using arrays * * Input: Global Variables * * * * Output: displayes global variable so no real output * ******************************************************************/ void encode( void ) { printf("Enter message to encode:"); scanf("%c", &junk); fgets(message, 500, stdin); length=(strlen(message)); for (counter=0; counter < length; counter = counter + 1) { int found = 0; for (counter2=0; counter2<=25; counter2 = counter2 + 1) { if (message[counter]==alphabet[counter2]) { printf("%c", cipher[counter2]); found = 1; } } if (!found) printf("%c", message[counter]); } } /****************************************************************** * * * Programmer: Matthew Glynn * * Name: decode * * Function: Decodes message using pointer arithmetic * * Input: Global Variables * * * * Output: displayes global variable so no real output * ******************************************************************/ void decode( void ) { printf("Enter message to decode:"); scanf("%c", &junk); fgets(message, 500, stdin); length=(strlen(message)); for (counter=0; counter < length; counter = counter + 1) { int found = 0; for (counter2=0; counter2<=25; counter2 = counter2 + 1) { if (*(messagePtr+counter)==*(cipherPtr+counter2)) { printf("%c", *(alphabetPtr+counter2)); found = 1; } } if (!found) printf("%c", *(messagePtr+counter)); } }