Advertisement
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>
- #include <stdlib.h>
- int main(int argc, string argv[])
- {
- int key = 0;
- //Check that only one command line argument was entered, otherwhise prompt "Usage: ./caesar key"
- if (argc != 2)
- {
- printf("Usage: ./caesar key\n");
- return 1;
- }
- else
- {
- // loop through the command line arguments
- for (int i = 1; i < argc; i++)
- {
- // loop through the characters of the command line argument
- for (int j = 0, n = strlen(argv[i]); j < n; j++)
- {
- // check whether the given character is a number - if not, print "not a digit key"
- if (!isdigit(argv[i][j]))
- {
- printf("not a digit key\n");
- return 1;
- }
- }
- key = atoi(argv[i]);
- //printf("Success\n%i\n", key);
- }
- }
- string ptext = get_string("plaintext: ");
- printf("ciphertext: ");
- for (int k = 0, n = strlen(ptext); k < n; k++)
- {
- // check if plain text is lowercase and convert the characters
- if islower(ptext[k])
- {
- printf("%c", (((ptext[k] + key) - 97) % 26) + 97);
- }
- // check if plain text is uppercase and convert the characters
- else if isupper(ptext[k])
- {
- printf("%c", (((ptext[k] + key) - 65) % 26) + 65);
- }
- // for any ASCII value outside of a-z and A-Z just print the value without any conversions
- else
- {
- printf("%c", ptext[k]);
- }
- }
- printf("\n");
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement