Skip to content

Instantly share code, notes, and snippets.

@chrisdel101
Last active February 19, 2018 04:07
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 chrisdel101/ecaa806ebfb0036181b113705d0b932a to your computer and use it in GitHub Desktop.
Save chrisdel101/ecaa806ebfb0036181b113705d0b932a to your computer and use it in GitHub Desktop.
// cleaned up with counter
int main(int argc, char *argv[]){
if(argc != 2) {
printf("Error on input");
exit(1);
}
string input = get_string("Input:");
printf("plaintext: %s\n", input);
printf("ciphertext: ");
string key = argv[1];
int len = strlen(key);
int keys[len];
int counter = 0;
for (int i = 0; i < strlen(key); i++) {
if(isupper(key[i])) {
keys[i] = key[i] - 65;
} else {
keys[i] = key[i] - 97;
}
}
for(int i = 0; i < strlen(input); i++) {
if(isalpha(input[i])) {
// if upper case to this
if(isupper(input[i])) {
// get keys from array above
int convertKey = keys[counter % (strlen(key))];
int convertInput = input[i] - 'A';
int result = (convertInput + convertKey) % 26 + 'A';
printf("%c", result);
counter++;
if(counter > len-1){
counter = 0;
}
} else if(!isupper(input[i])) {
int convertKey = keys[counter % (strlen(key))];
int convertInput = input[i] - 'a';
int result = (convertInput + convertKey) % 26 + 'a';
printf("%c", result);
counter++;
if(counter > len-1){
counter = 0;
}
}
} else if(!isalpha(input[i])) {
printf("%c", input[i]);
}
}
printf("\n");
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment