Last active
May 7, 2020 18:44
-
-
Save Angela-Ts/5a0ac57ed97ce76daaaefc2d314cd3b8 to your computer and use it in GitHub Desktop.
My implementation of cs50 pset2 (2020) "Caesar"
This file contains 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
#include <cs50.h> | |
#include <stdio.h> | |
#include <stdlib.h> | |
#include <ctype.h> | |
#include <string.h> | |
int main(int argc, string argv[]) | |
{ | |
// Accept only one command-line argument | |
if (argc == 2) | |
{ | |
// Check that they all chars are digits | |
int j = 0; | |
string z = argv[1]; | |
for (int i = 0, n = strlen(z); i < n; i++) | |
{ | |
if (isdigit(z[i])) | |
{ | |
j++; | |
} | |
} | |
// Convert according to the key | |
if (j == strlen(z)) | |
{ | |
int k = atoi(argv[1]); | |
string text = get_string("plaintext: "); | |
printf("ciphertext: "); | |
for (int l = 0, m = strlen(text); l < m; l++) | |
{ | |
// Convert | |
if islower(text[l]) | |
{ | |
printf("%c", (((text[l] - 'a') + k) % 26) + 'a'); | |
} | |
else if isupper(text[l]) | |
{ | |
printf("%c", (((text[l] - 'A') + k) % 26) + 'A'); | |
} | |
else | |
{ | |
printf("%c", text[l]); | |
} | |
} | |
printf("\n"); | |
return 0; | |
} | |
else | |
{ | |
printf("Usage: ./caesar key\n"); | |
return 1; | |
} | |
} | |
else | |
{ | |
printf("Please provide the key.\n"); | |
return 1; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment