Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <cs50.h>
- #include <string.h>
- #include <ctype.h>
- int main(int argc, string argv[])
- {
- string k = argv[1];
- //k will be the key
- //this for loop checks if all of the characters are in the alphabet
- int l = strlen(k);
- for (int i = 0; i < l; i++)
- {
- if (isupper(k[i]) || islower(k[i]))
- {
- }
- else
- {
- printf ("error!\n");
- return 1;
- }
- }
- //this if checks if argc equals 2 which means the user has given a correct key
- if (argc != 2)
- {
- printf ("error!\n");
- return 1;
- }
- string s = get_string("plaintext:");
- for (int j = 0; j < strlen(s); j++)
- {
- int m = j % l;
- if (isupper(s[j]))
- {
- k[m] -= 65;
- s[j] += k[m];
- if (s[j] > 90)
- {
- s[j] -= 26;
- }
- }
- if (islower(s[j]))
- {
- k[m] -= 97;
- s[j] = s[j] + k[m] - 26;
- if (s[j] < 97)
- {
- s[j] += 26;
- }
- }
- }
- printf("ciphertext:%s\n", s);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement