Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <cs50.h>
- #include <ctype.h>
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- int main(int argc, string argv[])
- {
- //the key
- int key;
- //key = atoi(argv[1]);
- //taking key value
- if (argc == 2)
- {
- key = atoi(argv[1]);
- }
- else
- {
- //nothing importsnt. just for later use -_-
- //but also helps with no command line argument
- //key = get_int("Enter key: ");
- return 1;
- }
- string pt = get_string("plaintext: ");
- char ct[strlen(pt) + 1];
- //so it will round to A, not some / or [
- key = (key % 26);
- for (int i = 0, n = strlen(pt); i < n; i++)
- {
- if (isalpha(pt[i]))
- {
- //checking if inside the Alphabet bound
- if (((pt[i] + key) > 64 && (pt[i] + key) < 91) || ((pt[i] + key) > 96 && (pt[i] + key) < 123))
- {
- ct[i] = pt[i] + key;
- }
- else
- {
- //taking care of out of out of bound
- ct[i] = (pt[i] + key) - 26;
- }
- }
- else
- {
- ct[i] = pt[i];
- }
- ct[strlen(pt)] = '\0';
- }
- //printf("ciphertext: ");
- printf("%s%s\n", "ciphertext: ", ct);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement