Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <cs50.h>
- #include <stdio.h>
- #include <string.h>
- #include <ctype.h>
- int k;
- int shift(char c);
- int main(int argc, string argv[])
- {
- if (argc == 2) // ensure one argument from user
- {
- for (int i = 0; i < strlen(argv[1]); i++) // iterate over length of argument input by user
- {
- if (isalpha(argv[1][i]) == 0) // ensure each character in argument is alphabetic
- {
- printf("Usage: ./vigenere keyword\n"); // error/failure message to user
- return 1;
- }
- }
- }
- string plain = get_string("plaintext: "); // get plaintext input from user
- printf("ciphertext: ");
- for (int i = 0; i < strlen(plain); i++) // iterate over length of plaintext input
- {
- k = shift(argv[1][i]);
- char c = plain[i]; // convert string to individual characters
- if (c < 65 || (c > 90 && c < 97) || c > 122) // leave non-alphabetical characters unchanged
- {
- printf("%c", c);
- }
- else if (((c + k % 26) > 90 && (c + k % 26) < 97) || (c + k % 26) > 122)
- // prevent printing of undesired non-alphabetical characters resulting from rotation
- {
- printf("%c", c + (k % 26 - 26));
- }
- else
- {
- printf("%c", c + (k % 26)); // apply key upon print
- }
- printf("\n");
- }
- }
- int shift(char c)
- {
- if (isupper(c))
- {
- int x = (c - 65);
- return x;
- }
- else
- {
- int x = (c - 97);
- return x;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement