Created
September 26, 2015 02:00
-
-
Save mxrss/0bb43e4ca0559ea09c38 to your computer and use it in GitHub Desktop.
Caesar
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
/* | |
Description: Takes in a input and outputs a "secret" output for easy | |
encryption. | |
*/ | |
#include <stdlib.h> | |
#include <stdio.h> | |
#include <cs50.h> | |
#include <string.h> | |
#include <ctype.h> | |
/* | |
Displays a encrypted character using the original character and key | |
*/ | |
void Display(char orginalCharacter, int key); | |
/* | |
Main program run | |
*/ | |
int main(int argc, char* argv[]) | |
{ | |
if (argc != 2) | |
{ | |
printf("usage ceaser {NUM} \n"); | |
return 1; | |
} | |
int offset = atoi(argv[1]); | |
string message = GetString(); | |
for (int counter=0; counter != strlen(message); counter++) | |
{ | |
if (isalpha(message[counter])) | |
{ | |
Display(message[counter], offset); | |
} | |
else | |
{ | |
printf("%c", message[counter]); | |
} | |
} | |
printf("\n"); | |
} | |
void Display(char orginalCharacter, int decoderKey) | |
{ | |
string alphabet = "abcdefghijklmnopqrstuvwxyz"; | |
int key = ( (tolower (orginalCharacter) - 'a') + decoderKey) % 26; | |
char result = alphabet[key]; | |
if (isupper(orginalCharacter)) | |
{ | |
result = toupper(result); | |
} | |
printf("%c", result); | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment