Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- Author: Justin Wheeler
- Date: March 7th, 2016
- Purpose: This script will take in a keyword and ask for the user to type in a string once executed.
- Purpose: After the user complies the program will shuffle the letters in the typed in string by the
- Purpose: values in the passed in keyword.
- */
- //------------------------------------------------------------
- //Libraries
- #include <cs50.h>
- #include <stdio.h>
- #include <string.h>
- #include <ctype.h>
- //------------------------------------------------------------
- //Method Signatures
- void killProgram(string _output);
- void printCipherLetter(int _arr1[], int _arr2[], int _letter, int _keyIndex, string _keycode);
- int checkAlpha(string _value);
- //------------------------------------------------------------
- //Main
- int main(int argc, string argv[])
- {
- if(argc == 2)
- {
- //Grab Keycode
- string keycode = argv[1];
- if(checkAlpha(keycode) == 0)
- {
- //Alphabets
- int alphabet1[26] = {'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P',
- 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'};
- int alphabet2[26] = {'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p',
- 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'};
- //Get String to Encrypt
- string userStr;
- userStr = GetString();
- //KeyCode Variables
- int keyBase = strlen(keycode);
- int keyCeil = strlen(keycode);
- int keyIndex = 0;
- //Cycle Through 'userStr' and Cipher Letters
- for(int i = 0; i < strlen(userStr) + 1; i++)
- {
- if(i == strlen(userStr))
- {
- printf("\n");
- }
- else
- {
- if(isalpha(userStr[i]))
- {
- keyIndex = keyBase % keyCeil;
- //Uppercase/Uppercase Condition
- if(isupper(userStr[i]) && isupper(keycode[keyIndex]))
- {
- printCipherLetter(alphabet1, alphabet1, userStr[i], keyIndex, keycode);
- }
- //Uppercase/Lowercase Condition
- else if(isupper(userStr[i]) && islower(keycode[keyIndex]))
- {
- printCipherLetter(alphabet1, alphabet2, userStr[i], keyIndex, keycode);
- }
- //Lowercase/Uppercase Condition
- else if (islower(userStr[i]) && isupper(keycode[keyIndex]))
- {
- printCipherLetter(alphabet2, alphabet1, userStr[i], keyIndex, keycode);
- }
- //Lowercase/Lowercase Condition
- else
- {
- printCipherLetter(alphabet2, alphabet2, userStr[i], keyIndex, keycode);
- }
- keyBase++;
- }
- //Special Characters Condition
- else
- {
- printf("%c", userStr[i]);
- }
- }
- }
- }
- else
- {
- killProgram("Provide an Alpha Key!\n");
- return 1;
- }
- }
- else
- {
- killProgram("Useage ./vigenere {string key}!\n");
- return 1;
- }
- return 0;
- }
- //------------------------------------------------------------
- //Methods
- void killProgram(string _output)
- {
- printf("%s\n", _output);
- }
- void printCipherLetter(int _arr1[], int _arr2[], int _letter, int _keyIndex, string _keycode)
- {
- //Cipher Variables
- int indexLetter = 0;
- int indexKey = 0;
- int cipherLetter = 0;
- //Letter Conversion
- for(int j = 0; j < 26; j++)
- {
- if(_letter == _arr1[j])
- {
- indexLetter = j;
- break;
- }
- }
- //Key Conversion
- for(int k = 0; k < 26; k++)
- {
- if(_keycode[_keyIndex] == _arr2[k])
- {
- indexKey = k;
- break;
- }
- }
- //Cipher Output
- cipherLetter = indexLetter + indexKey;
- while(cipherLetter > 25)
- {
- cipherLetter -= 26;
- }
- printf("%c", _arr1[cipherLetter]);
- }
- int checkAlpha(string _val)
- {
- for(int p = 0, len = strlen(_val); p < len; p++)
- {
- if(isalpha(_val[p]) == 0)
- {
- return 1;
- }
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement