/* Written by Kevin Klenk */ /* May 5th, 1998 */ /* cmps012a - Spring 1998 */ /* Ira Pohl */ /* This program inputs characters using getchar() until EOF is */ /* encountered. It encrypts each character if it is an alphabetic */ /* character using the following mapping: */ /* 'a' - 'z' to 'a' - 'z' */ /* 'A' - 'Z' to 'A' - 'Z' */ #include #include #include #define SEED 5 int main(void) { char c, encryptedc; /* Character read from input, encrypted character */ /* Initialize the random number generator with SEED. */ /* This must be done so that the pseudo-random number generator has a */ /* place to start. It does not matter what value SEED is as long as */ /* it is a valid integer. Also, you should know that, if you try to */ /* decrypt your encrypted string you have to know the seed. */ srand(SEED); /* Begin main loop. Read characters and encrypt them as long as EOF is */ /* not reached. Note: only alphabetic characters are encrypted. */ printf("Enter some characters (Ctrl-d to exit): "); while ( (c = getchar()) != EOF) { if (isupper(c)) { /* Character c is an uppercase letter. */ /* Encrypt it to another uppercase letter. */ encryptedc = (c + rand()) % 26 + 65; } else if (islower(c)) { /* Character c is a lowercase letter. */ /* Encrypt it to another lowercase letter. */ encryptedc = (c + rand()) % 26 + 97; } else { /* Character c is not alphabetic (not a letter). */ /* Do not encrypt the character. */ encryptedc = c; } putchar(encryptedc); } return 0; }