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[])
- {
- //creates alphabetical indexes for both upper and lowercased letters.
- string alphaUpper = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
- string alphaLower = "abcdefghijklmnopqrstuvwxyz";
- int cipherKey;
- int j = 0;
- if (argc == 2)
- {
- string key = argv[1];
- printf("Argv: %s\n", key);
- //gets input for encryption
- string ptext = get_string("Input: ");
- printf("ciphertext: ");
- //loops thru each character
- for (int i = 0, n = strlen(ptext); i < n; i++)
- {
- //B-A-C-O-N
- //1-0-2-14-13
- printf("J: %i\n", j);
- char c = ptext[i];
- if (isalpha(c))
- {
- //sets the cipherkey based on whether it's upper or lower case
- if (isupper(key[j]))
- {
- cipherKey = key[j % strlen(key)] - 65;
- printf("CKey: %i\n", cipherKey);
- }
- else
- {
- cipherKey = key[j % strlen(key)] - 97;
- printf("CKey: %i\n", cipherKey);
- }
- //DEFINE THE KEY
- //converts ASCII value to match alphabetical index based on case
- //applies key, then prints result
- if (isupper(c))
- {
- int alphaIndex = c - 65;
- int answer = (alphaIndex + cipherKey) % 26;
- printf("%c", alphaUpper[answer]);
- }
- else
- {
- int alphaIndex = c - 97;
- int answer = (alphaIndex + cipherKey) % 26;
- printf("%c", alphaLower[answer]);
- }
- j++;
- }
- //prints if character is not a letter(i.e. commas, spaces, etc)
- else
- {
- printf("%c", c);
- }
- }
- //pages breaks at end for cleanliness
- printf("\n");
- }
- //Prints error if too many or invalid arguments are given.
- else
- {
- printf("ERROR: Invalid input! Argument must be only one number!\n");
- return 1;
- }
- }
Add Comment
Please, Sign In to add comment