rot13(1)
This commit is contained in:
+41
@@ -0,0 +1,41 @@
|
||||
#include <ctype.h> /* isalpha(3), isupper(3) */
|
||||
#include <stdio.h> /* getc(3), putc(3) */
|
||||
#include <stddef.h> /* stdin, stdout, EOF */
|
||||
#include <string.h> /* strchr(3), strlen(3) */
|
||||
#include <sysexits.h> /* EX_OK, EX_USAGE */
|
||||
#include <unistd.h> /* write(2) */
|
||||
#include "libio.h" /* fdputs(3) */
|
||||
#include "usefulmacros.h" /* ARRAYLEN */
|
||||
|
||||
static char *program_name = "rot13";
|
||||
|
||||
/* Assumes this is a character in the provided glyph system.
|
||||
* Probably not Unicode-capable but doesn't assume Alphabet.
|
||||
* Returns `c` rotated around `a` by `r` characters. */
|
||||
static int
|
||||
caesar(char c, int r, const char *a){
|
||||
return a[(strchr(a, c) - a + r) % strlen(a)];
|
||||
}
|
||||
|
||||
/* It's faster to assume Alphabet. */
|
||||
static char alpha_upper[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLM";
|
||||
static char alpha_lower[] = "abcdefghijklmnopqrstuvwxyzabcdefghijklm";
|
||||
|
||||
int main(int argc, char *argv[]){
|
||||
char *a;
|
||||
int c;
|
||||
|
||||
if(argc > 1){
|
||||
write(2, "Usage: ", 7);
|
||||
fdputs(2, argv[0] == NULL ? program_name : argv[0]);
|
||||
return EX_USAGE;
|
||||
}
|
||||
|
||||
while((c = getc(stdin)) != EOF){
|
||||
if(isalpha(c)){
|
||||
a = isupper(c) ? alpha_upper : alpha_lower;
|
||||
putc(a[c - a[0] + 13], stdout);
|
||||
}else
|
||||
putc(c, stdout);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user