Skip to content

Instantly share code, notes, and snippets.

@Angela-Ts
Last active May 7, 2020 18:44
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 Angela-Ts/5a0ac57ed97ce76daaaefc2d314cd3b8 to your computer and use it in GitHub Desktop.
Save Angela-Ts/5a0ac57ed97ce76daaaefc2d314cd3b8 to your computer and use it in GitHub Desktop.
My implementation of cs50 pset2 (2020) "Caesar"
#include <cs50.h>
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <string.h>
int main(int argc, string argv[])
{
// Accept only one command-line argument
if (argc == 2)
{
// Check that they all chars are digits
int j = 0;
string z = argv[1];
for (int i = 0, n = strlen(z); i < n; i++)
{
if (isdigit(z[i]))
{
j++;
}
}
// Convert according to the key
if (j == strlen(z))
{
int k = atoi(argv[1]);
string text = get_string("plaintext: ");
printf("ciphertext: ");
for (int l = 0, m = strlen(text); l < m; l++)
{
// Convert
if islower(text[l])
{
printf("%c", (((text[l] - 'a') + k) % 26) + 'a');
}
else if isupper(text[l])
{
printf("%c", (((text[l] - 'A') + k) % 26) + 'A');
}
else
{
printf("%c", text[l]);
}
}
printf("\n");
return 0;
}
else
{
printf("Usage: ./caesar key\n");
return 1;
}
}
else
{
printf("Please provide the key.\n");
return 1;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment