Skip to content

Instantly share code, notes, and snippets.

@sameekapdi
Created October 2, 2017 21:40
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 sameekapdi/c94302a442c48ba0b3327366fba83bd9 to your computer and use it in GitHub Desktop.
Save sameekapdi/c94302a442c48ba0b3327366fba83bd9 to your computer and use it in GitHub Desktop.
#include <cs50.h>
#include <ctype.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int main(int argc, string argv[])
{
//CHeck to see if the correct number of arguments have been entered.
if (argc != 2)
{
printf("Please enter the correct number of arguments\n");
return 1;
}
//Convert argument from string to int
int key = atoi(argv[1]);
//Get user input for plaintext
printf("plaintext: ");
string plaintext = get_string();
if (plaintext != NULL)
{
printf("ciphertext: ");
//Iterate through the string and convert each letter
for (int i = 0, n = strlen(plaintext); i < n; i++)
{
//Check for lowercase
if (islower(plaintext[i]))
{
printf("%c", ((((plaintext[i] - 97) + key) % 26) + 97));
}
//check for uppercase
else if(isupper(plaintext[i]))
{
printf("%c", ((((plaintext[i] - 65) + key) % 26) + 65));
}
//Print non-alpha characters as they are
else
{
printf("%c", plaintext[i]);
}
}
}
//Print new line to end the deciphered text.
printf("\n");
//Exit the program
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment