Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <ctype.h>
- #include <cs50.h>
- #include <stdio.h>
- #include <string.h>
- #include <stdlib.h>
- #include <math.h>
- int main(int argc, string argv[])
- {
- // ensure that user input a key
- if (argc > 1 && argc < 3)
- {
- int s = strlen(argv[1]);
- // create array for string
- int key[s];
- // get plaintext string from user
- string m = GetString();
- // do for the length of plaintext string
- for (int j = 0, i = 0, num = strlen(m); j <= num; j++, i++)
- {
- // check case and validitiy of char in array
- if (islower(argv[1][j]))
- {
- // assign jth char of key to jth char of argv
- key[j] = (argv[1][j] - 97);
- // check message case for ith char
- if((m[i] >= 'A') && (m[i] <= 'Z'))
- {
- m[i] = ((m[i] -'A') + key[j % strlen(argv[1])]) % 26 +'A';
- printf("%c", m[i]);
- }
- if((m[i] >= 'a') && (m[i] <= 'z'))
- {
- m[i] = ((m[i] -'a') + key[j % strlen(argv[1])]) % 26 +'a';
- printf("%c", m[i]);
- }
- }
- else if (isupper(argv[1][j]))
- {
- key[j] = (argv[1][j] - 65);
- if((m[i] >= 'A') && (m[i] <= 'Z'))
- {
- m[i] = ((m[i] -'A') + key[j % strlen(argv[1])]) % 26 +'A';
- printf("%c", m[i]);
- }
- if((m[i] >= 'a') && (m[i] <= 'z'))
- {
- m[i] = ((m[i] -'a') + key[j % strlen(argv[1])]) % 26 +'a';
- printf("%c", m[i]);
- }
- }
- }
- printf("\n");
- return 0;
- }
- else
- printf("You must enter a key!\n");
- return 1;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement