Last active
April 7, 2018 17:53
-
-
Save prateek-parashar/f6eb77afe941704275b5cde0b42c4890 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # importing module to accept command line arguements | |
| import sys | |
| # importing cs50 module | |
| import cs50 | |
| if len(sys.argv) is not 2: | |
| print("Usage python caesar.py key") | |
| sys.exit(0) | |
| # marking the key given by the user | |
| key = int(sys.argv[1]) | |
| # initialising the list of ciphered text | |
| cipher = list() | |
| # asking user for the input | |
| text = cs50.get_string("Plaintext: ") | |
| length = len(text) | |
| # iterating over the length of plaintext | |
| for i in range(length): | |
| base = ord(text[i]) | |
| # encrypting the alphabets | |
| if text[i].isalpha(): | |
| if text[i].isupper(): | |
| base = base - ord("A") | |
| base = (base + key) % 26 | |
| base = base + ord("A") | |
| cipher.append(chr(base)) | |
| elif text[i].islower: | |
| base = base - ord("a") | |
| base = (base + key) % 26 | |
| base = base + ord("A") | |
| cipher.append(chr(base)) | |
| # appending characters that are not alphabets | |
| else: | |
| cipher.append(chr(base)) | |
| # creating a string from the list | |
| final = ''.join(cipher) | |
| # printing the ciphered text | |
| print(f"ciphertext: {final}") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment