Skip to content

Instantly share code, notes, and snippets.

@sheeit
Created July 19, 2017 01:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sheeit/e0f134afb2fce48d79937ccd5c3a4b10 to your computer and use it in GitHub Desktop.
Save sheeit/e0f134afb2fce48d79937ccd5c3a4b10 to your computer and use it in GitHub Desktop.
#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
char rot13_letter(char letter);
int main(int argc, char **argv)
{
int i;
char *c;
for (i = 1; i < argc; ++i) {
c = argv[i];
while (*c)
fputc(rot13_letter(*(c++)), stdout);
fputc(i == argc - 1 ? '\n' : ' ', stdout);
}
exit(EXIT_SUCCESS);
}
char rot13_letter(char letter)
{
short int cmp_letter = 13;
short int rot13_offset;
char rot13 = letter;
if (!isalpha(letter))
return letter;
cmp_letter += isupper(letter) ? 'A' : 'a';
rot13_offset = letter < cmp_letter ? 1 : -1;
rot13 += (char) (13 * (int) rot13_offset);
if (!isalpha(rot13) || isupper(letter) != isupper(rot13))
return '?';
return rot13;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment