Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <cs50.h>
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- //Daniel Lawson
- void getCipher(string text, int shift);
- int main(int argc, string argv[])
- {
- if (argc != 2) //make sure only 2 arguments are entered
- {
- printf("Please only print one addtional argument (./caesar 14)\n");
- return 1;
- }
- int shift = atoi(argv[1]); //get the "shift" value
- printf("plaintext: ");//ask for plaintext
- string text = get_string();//store it
- getCipher(text, shift);//call the cipher method, which converts text into ciphertext
- printf("ciphertext: %s\n", text); //print the cipher
- return 0;
- }
- //I made a method b/c to have for the next problem, so I can just copy and paste
- void getCipher(string text, int shift)
- {
- int length = strlen(text); //store the length of the string
- for (int i = 0; i < length; i++)//iterate over each character
- {
- if (text[i] != ' ') //don't do anything if the current character is a char
- {
- int originalChar = (int) text[i]; //get the char, and conver to decimal
- int fromZero; //will store the from 0 value of the char
- int caseValue = 0; //will store the decimal value for if the character is capital or not
- if (originalChar >= 97)
- {
- caseValue = 97;
- }
- else if (originalChar >= 65)
- {
- caseValue = 65;
- }
- fromZero = originalChar - caseValue; //make fromZero equal to the original minus the case value
- fromZero = (fromZero + shift) % 26; //add shift to fromZero, then % 26
- text[i] = (char) caseValue +
- fromZero; //add fromZero to caseValue and return to char, and replace the former value of text
- }
- }
- }
- //A starts at 65, a starts at 97
- /* sample output
- ~/workspace/pset2 $ ./caesar 13
- Be sure to drink your Ovaltine!
- Or fher gb qevax lbhe Binygvar!
- */
- /*io
- plaintext:
- ciphertext:
- */
- /*sample potato algorithm
- ci = (pi + k) % 26
- */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement