Skip to content

Instantly share code, notes, and snippets.

@deeunix

deeunix/Caesar.c Secret

Created September 13, 2020 09:28
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 deeunix/14886d36f0e8ea083fc5c5c5870b5dab to your computer and use it in GitHub Desktop.
Save deeunix/14886d36f0e8ea083fc5c5c5870b5dab to your computer and use it in GitHub Desktop.
Collins Cs50 caesar solution
#include <cs50.h>
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <stdlib.h>
int main(int argc, string argv[])
{
if (argc != 2) //to check if command line argument is not equal to 2
{
printf("Usage: ./caesar key\n"); //print "Usage: ./caesar key if not equal to 2"
return 1;
}
int arg_length = strlen(argv[1]); //to get the length of string in command line argument
for (int i = 0; i < arg_length; i++) //iterate through the length of the argv[1]
{
if (!isdigit(argv[1][i])) //to check if character in argv[1] is not digit
{
printf("Usage: ./caesar key"); //if not print this //
return 1;
}
}
int key = atoi(argv[1]); //convert string to integer
string plaintext = get_string("plaintext: "); //get plaintext to encipher from user
printf("ciphertext: "); //print the encipher text here
int plaintext_length = strlen(plaintext); //to check the length of plaintext
for (int i = 0; i < plaintext_length; i++) //iterate through the length of plaintext
{
if (isupper(plaintext[i])) // if plaintext character is Upper case.......encipher it with this calculation
{
printf("%c", (((plaintext[i] - 65) + key) % 26) + 65); // encipher the text with the calculation
}
else if (islower(plaintext[i])) //if plaintext character is lower case.....encipher with the below calculation
{
printf("%c", (((plaintext[i] - 97) + key) % 26) + 97); //enciper the text with the calculation
}
else //if not Uppercase or Lowercase, print the character like that
{
printf("%c", plaintext[i]);
}
}
printf("\n"); //then new line
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment