/* File: crunch.c An implementation of the Captain Crunch secret decoder ring. */ #include #include "simpio.h" #include "genlib.h" #include "strlib.h" main() { string str ; int index, i ; char c ; /* Initialize our code */ char code[26] = {'t','f','h','x','q','j','e','m','u','p', 'i','d','c','k','v','b','a','o','l','r', 'z','w','g','n','s','y'} ; /* Get Input */ printf("Enter a line: ") ; str = GetLine() ; /* Encrypt */ for (i = 0 ; str[i] != '\0' ; i++) { if ( isalpha(str[i]) ) { c = tolower(str[i]) ; index = (int) c - 'a' ; str[i] = code[index] ; } } printf(" Encrypted: %s\n", str ) ; /* Decrypt */ for (i = 0 ; str[i] != '\0' ; i++) { if ( isalpha(str[i]) ) { c = tolower(str[i]) ; /* find matching character */ for (index = 0 ; index < 26 ; index++) { if ( code[index] == c ) { str[i] = (char) 'a' + index ; } /* if */ } /* for */ } /* if ( isalpha ... ) */ } /* for */ printf(" Decrypted: %s\n", str ) ; }