From 04d4aaf15ae7b20a2f6dad5a4a4a1e03b73a0db7 Mon Sep 17 00:00:00 2001 From: Ibrahim Ayad Date: Fri, 27 Sep 2019 14:07:41 +0300 Subject: [PATCH] Week2-caesar --- caesar.c | 37 +++++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) create mode 100644 caesar.c diff --git a/caesar.c b/caesar.c new file mode 100644 index 0000000..a75e3fa --- /dev/null +++ b/caesar.c @@ -0,0 +1,37 @@ +#include +#include +#include +#include + +int main(int argc, string argv[]) +{ + string p, c = ""; + if (argc == 2 && atoi(argv[1]) > 0)// check the arguments + { + int key = atoi(argv[1]);// use key + p = get_string("plaintext: ");// get the plaintext + printf("ciphertext: "); + for (int i = 0; i < strlen(p); i++) + { + if (p[i] >= 'a' && p[i] <= 'z') //check lowercase + { + printf("%c", (((p[i] - 'a') + key) % 26) + 'a'); + } + else if (p[i] >= 'A' && p[i] <= 'Z')//check uppercase + { + printf("%c", (((p[i] - 'A') + key) % 26) + 'A'); + } + else + { + printf("%c", p[i]); //otherways + } + } + printf("\n"); + } + else + { + printf("Usage: ./caesar key\n");//when the argc has not key + return 1; + } + +}