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>
- #include <stdlib.h>
- int k_lenain(int argc, string argv[])
- {
- //should key in valid "keyword"
- if (argc == 2)
- {
- //get key
- string key = argv[1];
- int k_len = strlen(key);
- int j = 0;
- int key_index = 0;
- if (key != NULL && isalpha(key[j]))
- {
- printf("plaintext: ");
- string p = get_string();
- printf("ciphertext: ");
- for (int i = 0, n = strlen(p); i < n; i++)
- {
- if (i == 0)
- {
- key_index = 0;
- }
- else if(p[i] != ' ')
- {
- if(key_index >= (k_len-1))
- {
- key_index = 0;
- }
- else
- {
- key_index++;
- }
- }
- if (isalpha(p[i]) && isalpha(key[key_index]))
- {
- if (isupper(p[i]) && isupper(key[key_index]))
- {
- char a = (((p[i]) - 65) + ((key[key_index]) - 65)) % 26;
- char b = a + 65;
- printf("%c", b);
- }
- if (isupper(p[i]) && islower(key[key_index]))
- {
- char a = (((p[i]) -65) + ((key[key_index]) - 97)) % 26;
- char b = a + 65;
- printf("%c", b);
- }
- if (islower(p[i]) && isupper(key[key_index]))
- {
- char a = (((p[i]) - 97) + ((key[key_index]) - 65)) % 26;
- char b = a + 97;
- printf("%c", b);
- }
- if (islower(p[i]) && islower(key[key_index]))
- {
- char a = (((p[i]) -97) + ((key[key_index]) - 97)) % 26;
- char b = a + 97;
- printf("%c", b);
- }
- }
- else
- {
- printf("%c", p[i]);
- }
- }
- printf("\n");
- }
- return 1;
- }
- return 1;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement