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 shift(char c);
- int main(int argc, string argv[])
- {
- // Check to see if two arguments are enterted at launch
- int cipher = 0;
- if (argc != 2)
- {
- // If not return error & return 0
- printf("Usage: ./vigenere keyword \n");
- return 1;
- }
- else
- {
- int strlength = strlen(argv[1]);
- // Iterates through characters in second argument (key), checking to see if they are digits
- for (int k = 0; k < strlength; k++)
- {
- if (isdigit(argv[1][k]))
- {
- // If not return error & return 1
- printf("Usage: ./vigenere keyword\n");
- return 2;
- }
- }
- //char *c =argv[1];
- string plaintext = get_string("Plaintext: ");
- int len = (int) strlen(plaintext);
- //int b = atoi(c);
- //char code[len];
- //strcpy (code, plaintext);
- int z=0;
- for (int j = 0; j < len; j++)
- {
- int key = shift(argv[1][z]);
- printf("%i",z);
- if (isupper(argv[1][z]))
- {
- //printf("theory\n");
- cipher = ((((plaintext[j] - 'A') + key) + 'A'));
- //cipher = ((((plaintext[j] - 'A') + key) % 26) + 'A');
- //printf("%c", (((plaintext[j] - 'A') + key) % 26) + 'A');
- //printf("%i",z);
- printf("%c",cipher);
- z++;
- if (z > strlen(argv[1])-1)
- {
- z=0;
- }
- }
- if (islower(argv[1][z]))
- {
- //printf("theory\n");
- cipher = (((plaintext[j] - 'a') + key) + 'a');
- //cipher = ((((plaintext[j]) -'a') + key) % 26) + 'a');
- //printf("%c", (((plaintext[j] - 'a') + key) % 26) + 'a');
- //printf("%i",z);
- printf("%c",cipher);
- z++;
- if (z > strlen(argv[1])-1)
- {
- z=0;
- }
- }
- //z++;
- //else if (!isalpha(plaintext[j]))
- //{
- //printf("%c", plaintext[j]);
- //}
- /* else
- {
- z++;
- }
- else
- {
- z++;
- }
- */
- //z++;
- }
- printf("\n");
- }
- }
- int shift(char c)
- {
- int i = c;
- if (i <= 'Z' && i >= 'A')
- {
- return ((i - 'A') % 26);
- }
- else
- {
- return ((i - 'a') % 26);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement