Skip to content

Instantly share code, notes, and snippets.

@OxiBo
Last active September 9, 2017 23:13
Show Gist options
  • Save OxiBo/8bd106063e45c71939649e715a265989 to your computer and use it in GitHub Desktop.
Save OxiBo/8bd106063e45c71939649e715a265989 to your computer and use it in GitHub Desktop.
CS50 week2/week8 caesar
/**
* CS50 week2 caesar
*/
#include <stdio.h>
#include <cs50.h>
#include <ctype.h>
#include <string.h>
#include <stdlib.h>
int main(int argc, string argv[])
{
int key;
// make sure a user gives a secret key from command line
if (argc!=2)
{
printf("missing command-line argument\n");
return 1;
}
else
{
//convert a string from argv[] to an integer (key)
key=atoi(argv[1]);
printf("plaintext:");
string plaintext=get_string();
if (plaintext!=NULL)
{
printf("ciphertext:");
//iterate over the characters in the plaintext one at a time
for(int i=0, n=strlen(plaintext); i<n; i++)
{
//check if [i] character is alphabetic character and uppercase
if (isalpha(plaintext[i])&&isupper(plaintext[i]))
{
//find alphabetic index of a character in plaintext
int alph_index;
alph_index=plaintext[i]%65;
//find and print corresponding character in ciphertext in ASCII in uppercase
int cyphertext;
cyphertext=65+(alph_index+key)%26;
printf("%c", cyphertext);
}
else
{
//check if [i] character is alphabetic character and lowercase
if (isalpha(plaintext[i])&&islower(plaintext[i]))
{
//find alphabetic index of a character in plaintext
int alph_index;
alph_index=plaintext[i]%97;
//find and print corresponding character in ciphertext in ASCII in lowercase
int cyphertext;
cyphertext=97+(alph_index+key)%26;
printf("%c", cyphertext);
}
else
{
//print [i] character if it is not alphabetic character
printf("%c", plaintext [i]);
}
}
}
}
}
printf("\n");
return 0;
}
# program that encrypts messages using Caesar’s cipher
def main():
import sys
import cs50
#make sure a user gives a secret key from command line
if len(sys.argv)!=2:
print("missing command-line argument")
exit (1)
else:
key = int(sys.argv[1]) #command-line argument gives a string, we use int to convert it to an integer
print("plaintext:", end="")
plaintext = cs50.get_string()
if plaintext != None:
print("ciphertext: ", end="")
#iterate over the characters in the plaintext one at a time
for char in plaintext:
#check if char character is alphabetic character and uppercase
if str.isalpha(char) and str.isupper(char):
#find alphabetic index of a character in plaintext
alph_index = ord(char)%65
#find and print corresponding character in ciphertext
cyphertext = chr((65+(alph_index+key)%26))
print(cyphertext, end="")
#check if char character is alphabetic character and lowercase
elif str.isalpha(char) and str.islower(char):
#find alphabetic index of a character in plaintext
alph_index = ord(char)%97
#find and print corresponding character in ciphertext
cyphertext = chr((97+(alph_index+key)%26))
print(cyphertext, end="")
else:
#print char character if it is not alphabetic character
print(char, end="")
print()
exit(0)
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment