UMBC CS 201, Fall 98
Captain Crunch Decoder Ring
Knowing that strings are really arrays allows us
to access the elements of the array and change them directly.
For example, we can now write an implementation of the Captain Crunch
secret decoder ring easily.
The Program
/* File: crunch.c
An implementation of the Captain Crunch
secret decoder ring.
*/
#include
#include
#define SIZE 25
main()
{
char c, str[SIZE] = "this is a secret message";
int index, i;
/* 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'} ;
/* Print the original phrase */
printf ("Original phrase: %s\n", str);
/* 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 ;
}
}
}
}
printf(" Decrypted: %s\n", str ) ;
}
The Sample Run
lassie% a.out
Original phrase: this is a secret message
Encrypted: rmul ul t lqhoqr cqllteq
Decrypted: this is a secret message
lassie%
CSEE
|
201
|
201 F'98
|
lectures
|
news
|
help
Last Modified - Monday, 26-Oct-1998 11:54:59 EST