Created
March 5, 2013 03:19
-
-
Save darkf/5087725 to your computer and use it in GitHub Desktop.
Cesar cipher in C
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* 2013/3/4 by darkf - licensed under WTFPL - cesar cipher program */ | |
#include <stdio.h> | |
#include <string.h> | |
int main(int argc, char *argv[]) { | |
int key, i, len; | |
char output[128]; | |
if(argc != 3) { | |
fprintf(stderr, "usage: %s CIPHER KEY\n", argv[0]); | |
return 1; | |
} | |
if(sscanf(argv[2], "%d", &key) != 1) { | |
fprintf(stderr, "invalid key: %s\n", argv[2]); | |
return 2; | |
} | |
if(strcmp(argv[1], "cesar") == 0) { | |
int c; | |
i = 0; | |
while((c = fgetc(stdin)) != EOF) | |
output[i++] = ((unsigned char) c) + key; | |
len = i; | |
} | |
else { | |
fprintf(stderr, "invalid cipher: %s\n", argv[1]); | |
return 2; | |
} | |
printf("output: "); | |
for(i = 0; i < len; i++) | |
printf("%02X (%c) ", output[i], output[i]); | |
printf("\n"); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment