Skip to content

Instantly share code, notes, and snippets.

@mxrss
Created September 26, 2015 02:00
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 mxrss/0bb43e4ca0559ea09c38 to your computer and use it in GitHub Desktop.
Save mxrss/0bb43e4ca0559ea09c38 to your computer and use it in GitHub Desktop.
Caesar
/*
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