This commit is contained in:
2022-08-23 11:40:45 -04:00
parent c838c0b304
commit 8c55735e67
2 changed files with 49 additions and 0 deletions
+41
View File
@@ -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);
}
}