Skip to content

Instantly share code, notes, and snippets.

@kaungmyatlwin
Last active June 25, 2017 06:20
Show Gist options
  • Save kaungmyatlwin/b14c7ea3483a09c807468b48610dab83 to your computer and use it in GitHub Desktop.
Save kaungmyatlwin/b14c7ea3483a09c807468b48610dab83 to your computer and use it in GitHub Desktop.
#include <cs50.h>
#include <string.h>
#include <ctype.h>
#include <stdlib.h>
#include <stdio.h>
char shiftCipher(char c, int k) {
if(isupper(c)) {
return ((c - 65 + k) % 26) + 65;
} else if (islower(c)){
return ((c - 97 + k) % 26) + 97;
} else {
return c;
}
}
int main(int argc, string argv[]) {
string text;
if(argc != 2) {
printf("Error. Please provide a cipher key.\n");
return 1;
}
do {
printf("plaintext: ");
text = get_string();
} while(strlen(text) <= 0);
char cipher[strlen(text)];
int key = atoi(argv[1]);
for(int i = 0; i < strlen(text); i++) {
cipher[i] = shiftCipher(text[i], key);
}
printf("ciphertext: %s", cipher);
printf("\n");
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment