/* This program performs a simple rotation of the alphabet on ** the standard input and writes it out on standard out. ** ** The command line arguments are: ** -n # where # is the rotation number. Default is 0. ** -l # use # as the alphabet length */ #include #include #define ALPHLENGTH 26 #define MAXLENGTH 256 #define DEFROT 0 #define TRUE 1 #define FALSE 0 void rotate_char(); int main(argc, argv) int argc; char **argv; { int rotnum = DEFROT; int manyflag = FALSE; int alphabet_length = ALPHLENGTH; char *word = NULL; char c; --argc, ++argv; while(argc){ if(**argv != '-'){ if(word){ (void) printf("Bad option: %s\n", *argv); } else{ word = *argv; } --argc, ++argv; } else{ switch(*++*argv){ case 'n': --argc, ++argv; if(sscanf(*argv, "%d", &rotnum) != 1){ (void) printf("Bad argument value.\n"); rotnum = DEFROT; } --argc, ++argv; break; case 'l': --argc, ++argv; if(sscanf(*argv, "%d", &alphabet_length) != 1){ (void) printf("Bad argument value.\n"); alphabet_length = ALPHLENGTH; } --argc, ++argv; break; } } } /* Now perform the rotation. */ rotnum %= alphabet_length; if(rotnum == 0) manyflag = TRUE; if(!word){ while( (c = getchar()) != EOF){ if(manyflag){ for(rotnum = 0; rotnum < alphabet_length; rotnum++){ if(c != '\n'){ rotate_char(c, rotnum, alphabet_length); (void) putchar(' '); } } (void) putchar('\n'); } else rotate_char(c, rotnum, alphabet_length); } } /* Otherwise the word was entered on the command line. */ else{ while(*word){ if(manyflag){ for(rotnum = 0; rotnum < alphabet_length; rotnum++){ rotate_char(*word, rotnum, alphabet_length); (void) putchar(' '); } (void) putchar('\n'); } else rotate_char(*word, rotnum, alphabet_length); word++; } (void) putchar('\n'); } } void rotate_char(c, rotnum, alphabet_length) char c; int rotnum; int alphabet_length; { if(isalpha(c)){ /* If c is in the first 26-n characters of the alphabet, then add ** n to its value. otherwise subtract 26-n from its value. */ if(isupper(c)) if( (int) c - 'A' < alphabet_length-rotnum) c += rotnum; else c += rotnum-alphabet_length; else if( (int) c - 'a' < alphabet_length-rotnum) c += rotnum; else c += rotnum-alphabet_length; } (void) putchar(c); }