Last active
August 29, 2015 14:03
-
-
Save jasonmoo/e50f2c9fdf4713f76f62 to your computer and use it in GitHub Desktop.
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
#include <stdio.h> | |
#include <stdlib.h> | |
#include <string.h> | |
#include <ctype.h> | |
#define MAX_MESSAGE_LENGTH 1024 | |
int main(int argc, char* argv[]) { | |
if (argc != 2) { | |
printf("Please select a key."); | |
return 1; | |
} | |
int key = atoi(argv[1]); | |
puts("What message would you like to encrypt?\n"); | |
char message[MAX_MESSAGE_LENGTH]; | |
char cipher[MAX_MESSAGE_LENGTH]; | |
fgets(message, MAX_MESSAGE_LENGTH, stdin); | |
int i = 0; | |
int length = strlen(message); | |
// trim the last char (newline) | |
length--; | |
message[length] = '\0'; | |
for(; i < length; i++) { | |
if(isalpha(message[i])) { | |
if(isupper(message[i])) { | |
cipher[i] = (message[i] - 65 + key) % 26 + 65; | |
} else if(islower(message[i])) { | |
cipher[i] = (message[i] - 97 + key) % 26 + 97; | |
} | |
} | |
printf("input char:%c cipher char:%c (ascii value: %d)\n", message[i], cipher[i], cipher[i]); | |
} | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment