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>
- void getCipherVaried(string text, string shifts);
- int getCaseValue(int originalChar);
- int main (int argc, string argv[])
- {
- if (argc != 2) //check for two arguments
- {
- printf("please enter two arguments (./vignere ABC)\n");
- return 1;
- }
- string shifts = argv[1];//get the shift arguments
- for (int i = 0; i < strlen(shifts); i++) //make sure each character in the shifts is in the alphabet
- {
- if (!isalpha(shifts[i]))
- {
- printf("please only enter letters!!!!! >:(\n");
- return 1;
- }
- }
- printf("plaintext: "); //ask and store plain text
- string text = get_string();
- getCipherVaried(text,
- shifts); //send plaintext and shifts to GetCipherVaried, which modifies the text into cyphertext based off the shifts
- printf("ciphertext:%s\n", text ); //print ciphertext
- return 0;
- }
- void getCipherVaried(string text, string shifts)
- {
- int length = strlen(text); //store the length of the text
- int counter = 0; //counter for changing shifts
- int shiftLength = strlen(shifts); //store the length of the shifts
- for (int i = 0; i < length; i++) //iterate over each character of the text
- {
- if (text[i] != ' '
- && isalpha(text[i])) //don't do anything if the current character is a char or not alpha
- {
- if (counter > shiftLength - 1) //reset the counter if outsie shifts length
- {
- counter = 0;
- }
- int shift = (int)(shifts[counter]);//get the current shift at the counter location
- shift -= getCaseValue(shift); //get the 0 base value of shift
- int originalChar = (int) text[i]; //get the char, and conver to decimal
- int fromZero = originalChar - getCaseValue(originalChar);
- fromZero = (fromZero + shift) % 26; //add shift to fromZero, then % 26
- text[i] = (char) (getCaseValue(originalChar) +
- fromZero); //add fromZero to caseValue and return to char, and replace the former value of text
- counter++;
- }
- }
- }
- int getCaseValue(int
- originalChar)//get the original char value and return it's case value (97 for lowercase, 65 capital)
- {
- int caseValue = 0;
- if (originalChar >= 97)
- {
- caseValue = 97;
- }
- else if (originalChar >= 65)
- {
- caseValue = 65;
- }
- return caseValue;
- }
- /*$ ./vigenere ABC
- plaintext: HELLO
- ciphertext: HFNLP*/
Advertisement
Add Comment
Please, Sign In to add comment