Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <cs50.h>
- #include <ctype.h>
- #include <stdio.h>
- #include <string.h>
- char shift_character(char c, char key);
- int main(int argc, string argv[])
- {
- if (argc != 2)
- {
- printf("Usage: ./vigenere k\n");
- return 1;
- }
- string key = argv[1];
- int key_index = 0;
- int max_index = strlen(key) - 1;
- for (int i = 0; i <= max_index; i++)
- {
- if (!isalpha(key[i]))
- {
- return 1;
- }
- }
- string plain_text = get_string("plaintext: ");
- printf("ciphertext: ");
- for (int i = 0, input_length = strlen(plain_text); i < input_length; i++)
- {
- if (!isalpha(plain_text[i]))
- {
- printf("%c", plain_text[i]);
- continue;
- }
- printf("%c", shift_character(plain_text[i], key[key_index]));
- key_index++;
- if (key_index > max_index)
- {
- key_index = 0;
- }
- }
- printf("\n");
- }
- char shift_character(char c, char key)
- {
- key = toupper(key) - 'A';
- int offset = ((islower(c)) ? 'a' : 'A');
- int alphabit_index = c - offset;
- int shifted_alpha_index = (alphabit_index + key) % 26;
- return shifted_alpha_index + offset;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement