Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <cs50.h>
- #include <stdio.h>
- #include <string.h>
- #include <stdlib.h>
- #include <ctype.h>
- int main(int argc, string argv[])
- {
- //Make sure that there is only 1 correct argument
- if (argc != 2)
- {
- printf("Usage: ./caesar k\n");
- return 1;
- }
- else
- {
- //Once I get the correct argument,k ,change the string into an int
- int k = atoi(argv[1]);
- //Make sure the integer is non-negative
- if (k < 0)
- {
- printf("Usage: ./caesar k , k must be more than 0\n");
- return 1;
- }
- else
- {
- //Prompt user for a code to encrypt
- string s = get_string("plaintext:");
- printf("ciphertext: ");
- for (int i = 0, n = strlen(s) ; i < n ; i++)
- {
- if (isalpha(s[i]))
- {
- //Encrypt Uppercase letter
- if (isupper(s[i]))
- {
- printf("%c", ((s[i] - 'A' + k) % 26) + 'A');
- }
- //Encrypt Lowercase Letter
- if (islower(s[i]))
- {
- printf("%c", ((s[i] - 'a' + k) % 26) + 'a');
- }
- }
- //Print back the text without changes
- else
- {
- printf("%c", s[i]);
- }
- }
- //Print a new line
- printf("\n");
- return 0;
- }
- }
- }
Add Comment
Please, Sign In to add comment