Skip to content

Instantly share code, notes, and snippets.

@RickyNotaro
Last active September 19, 2016 12:49
Show Gist options
  • Save RickyNotaro/1bf3b4bf217673496034 to your computer and use it in GitHub Desktop.
Save RickyNotaro/1bf3b4bf217673496034 to your computer and use it in GitHub Desktop.
(Cs50) Caesar
#include <stdio.h>
#include <stdlib.h>
#include <cs50.h>
#include <string.h>
#include <ctype.h>
int main(int input, string argv[])
{
if(input == 2)
{
int key = atoi(argv[1]);
if (key > 0)
{
string plaintext = GetString();
for (int currentchar = 0, n = strlen(plaintext); currentchar < n; currentchar++)
{
if(isalpha(plaintext[currentchar]))
{
int a = plaintext[currentchar];
char b;
if (isupper(a)) {b = 'A';}
else {b = 'a';}
plaintext[currentchar] = ((((plaintext[currentchar] + key)- b) % 26)+b);
}
printf("%c",plaintext[currentchar]);
}
printf("\n");
}
else
{
printf("<key> need to be a positive integer\n");
return 1;
}
}
else
{
printf("Invalid parameter !: caesar <key>\n");
return 1;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment