Skip to content

Instantly share code, notes, and snippets.

@DeclanGas
Created June 23, 2022 16:50
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 DeclanGas/dfaec3d1285b801aef86395d3f5cc212 to your computer and use it in GitHub Desktop.
Save DeclanGas/dfaec3d1285b801aef86395d3f5cc212 to your computer and use it in GitHub Desktop.
#include <stdio.h>
#include <cs50.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
int main(int argc, string argv[])
{
if (argc != 2)
{
printf("Wrong command\n");
return 1;
}
int k = atoi(argv[1]);
string a = argv[1];
for (int i = 0; i < strlen(a); i++)
{
if (a[i] < '0' || a[i] > '9')
{
printf("False\n");
return 1;
}
}
if (k < 0)
{
printf("Wrong command\n");
return 1;
}
else
{
string code = get_string("plaintext: ");
printf("ciphertext: ");
for (int i = 0, n = strlen(code); i < n; i++)
{
if (islower(code[i]))
{
printf("%c", (((code[i] + k) - 97) % 26) + 97);
}
else if (isupper(code[i]))
{
printf("%c", (((code[i] + k) - 65) % 26) + 65);
}
else
{
printf("%c", code[i]);
}
}
printf("\n");
return 0;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment