Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <string.h>
- #include <cs50.h>
- #include <ctype.h>
- //this function checks if the command line argument has any non-alphabetic characters
- int valid_command(string cmd);
- //this function converts the key to an array of ints that correspond to each character in the key (ex: A or a = 0, B or b = 1, etc.) and returns a pointer to that array
- int* convert_key (string cmd);
- //calculate the length of the string
- int length (string cmd);
- int main (int argc, string argv[]){
- //cmd is used as a bool for the following if-statement- It's used to check if the command line argument (key) has any non-alphabetic characters
- int cmd = valid_command(argv[1]);
- //error checking for if the user input more than 2 command line arguments or if the key has any non-alphabetic characters
- if ((argc != 2) || cmd){
- printf("Error: Not valid input!n");
- return 1;
- }
- else{
- printf("plaintext: ");
- string plaintext = get_string();
- int size = length(argv[1]); //save the length of the key
- //convert key to an array of integers
- int* delta = convert_key(argv[1]);
- int* cipher = (int*)malloc((size+1)*sizeof(int));
- int* cipher_ptr = cipher;
- char* p_ptr = plaintext;
- //go through each character in plaintext, determine if it's an alphabetic character and whether it's in the A-Z range or a-z range and use a calculation based on that to figure out how the character should be encrypted. This also takes into account wrapping of the key array (if the key array is smaller than plaintext, then once the last character in key is used to encrypt a character in plaintext, the next character should start over from the beginning of key) and also takes into account maintaining the case of the character of plaintext
- if (plaintext != NULL){
- int i = 0;
- while ((*p_ptr) != '