Skip to content

Instantly share code, notes, and snippets.

@hjpbarcelos
Last active December 15, 2015 22:29
Show Gist options
  • Save hjpbarcelos/5333156 to your computer and use it in GitHub Desktop.
Save hjpbarcelos/5333156 to your computer and use it in GitHub Desktop.
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <string.h>
#include <assert.h>
int main(int argc, char *argv[]) {
assert(argc == 3);
int i;
char key = tolower(argv[2][0]);
char op = tolower(argv[1][0]);
char buff[1000];
assert(key >= 'a' && key <= 'z');
while(fgets(buff, 1000, stdin)) {
for(i = 0; i < strlen(buff); i++) {
if(tolower(buff[i]) >= 'a' && tolower(buff[i]) <= 'z') {
char offset = buff[i] < 'a' ? 'A' : 'a';
char buffKey = buff[i] < 'a' ? toupper(key) : key;
// Obtém a chave simétrica
if(op == 'd' && buffKey != offset)
buffKey = 2 * offset + 26 - buffKey;
buff[i] = offset + (buff[i] + buffKey - 2 * offset) % 26;
}
}
fputs(buff, stdout);
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment