Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <cs50.h>
- #include <stdio.h>
- #include <ctype.h>
- #include <string.h>
- #include <stdlib.h>
- int main(int argc, string argv[1])
- {
- int key = atoi(argv[1]); //user input at start of program
- if (argc != 2 || key < 1) //if user doesn't run command-line argument of one or enter positive int
- {
- printf("Usage: ./caesar key\n"); //prompt them to run it again
- return 1; //returning zero means everything went well
- }
- string s = get_string("plaintext: "); //prompt for plaintext
- for (int i = 0; i < strlen(s); i++) //length of the entire string
- string cipher = [(((s[i] - 97) + key) % 26) + 97;]; //create new string, add key and rotate
- for (int j = 0; j < strlen(s); j++)
- if isupper(s[i])
- char c = [(((s[i] - 65) + key) % 26) + 65;]
- if islower(s[i])
- char c = [(((s[i] - 97) + key) % 26) + 97;]
- {
- printf("ciphertext: %c\n", cipher[j]); //print new string after rotating
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement