* Copyright (c) 1989 The Regents of the University of California.
* This code is derived from software contributed to Berkeley by
* Stan King, John Eldridge, based on algorithm suggested by
* %sccs.include.redist.c%
"@(#) Copyright (c) 1989 The Regents of the University of California.\n\
static char sccsid
[] = "@(#)caesar.c 5.4 (Berkeley) %G%";
#define ROTATE(ch, perm) \
isupper(ch) ? ('A' + (ch - 'A' + perm) % 26) : \
islower(ch) ? ('a' + (ch - 'a' + perm) % 26) : ch
* letter frequencies (taken from some unix(tm) documentation)
* (unix is a trademark of Bell Laboratories)
7.97, 1.35, 3.61, 4.78, 12.37, 2.01, 1.46, 4.49, 6.39, 0.04,
0.42, 3.81, 2.69, 5.92, 6.96, 2.91, 0.08, 6.63, 8.77, 9.68,
2.62, 0.81, 1.88, 0.23, 2.07, 0.06,
register int ch
, dot
, i
, nread
, winnerdot
;
int obs
[26], try, winner
;
char *malloc(), *strerror();
if (!(inbuf
= malloc(LINELENGTH
))) {
(void)fprintf(stderr
, "caesar: out of memory.\n");
/* adjust frequency table to weight low probs REAL low */
stdf
[i
] = log(stdf
[i
]) + log(26.0 / 100.0);
/* zero out observation table */
bzero(obs
, 26 * sizeof(int));
if ((nread
= read(STDIN_FILENO
, inbuf
, LINELENGTH
)) < 0) {
(void)fprintf(stderr
, "caesar: %s\n", strerror(errno
));
* now "dot" the freqs with the observed letter freqs
* and keep track of best fit
for (try = winner
= 0; try < 26; ++try) { /* += 13) { */
dot
+= obs
[i
] * stdf
[(i
+ try) % 26];
/* initialize winning score */
for (i
= 0; i
< nread
; ++i
) {
putchar(ROTATE(ch
, winner
));
if ((nread
= read(STDIN_FILENO
, inbuf
, LINELENGTH
)) < 0) {
(void)fprintf(stderr
, "caesar: %s\n", strerror(errno
));
if ((rot
= atoi(arg
)) < 0) {
(void)fprintf(stderr
, "caesar: bad rotation value.\n");
while ((ch
= getchar()) != EOF
)
putchar(ROTATE(ch
, rot
));