Skip to content

Instantly share code, notes, and snippets.

@caseyscarborough
Created January 14, 2014 14:34
Show Gist options
  • Save caseyscarborough/8419205 to your computer and use it in GitHub Desktop.
Save caseyscarborough/8419205 to your computer and use it in GitHub Desktop.
Encrypts text using the rot13 cipher.
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>
char *convert_to_rot13(char *str) {
size_t i;
for (i = 0; i < strlen(str); i++) {
if (str[i] > 64 && str[i] < 91) {
str[i] += str[i] < 78 ? 13 : -13;
} else if (str[i] > 96 && str[i] < 123) {
str[i] += str[i] < 110 ? 13 : -13;
}
}
return str;
}
void die(char *error)
{
if (errno) {
printf("%d\n", errno);
} else {
printf("%s\n", error);
}
exit(-1);
}
int main(int argc, char **argv)
{
FILE *ifp, *ofp;
char word [100];
if (argc != 3) {
die("This script requires only two arguments.");
} else {
ifp = fopen(argv[1], "r");
ofp = fopen(argv[2], "w");
if (ifp == NULL) {
die("Error opening input file!");
} else if (ofp == NULL) {
die("Error opening output file!");
}
while (fgets(word, 100, ifp) != NULL) {
fprintf(ofp, "%s", convert_to_rot13(word));
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment