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>
- int main(int argc, string argv[])
- {
- // Print error message if more/less than one command line argument is given
- if (argc != 2)
- {
- printf("Error\n");
- return 1;
- }
- // If one command line argument is given, turn the string into an int to give key k
- int k = atoi(argv[1]);
- // Error message k is a negative integer
- if (k < 0)
- {
- printf("Error\n");
- return 1;
- }
- else
- {
- // Prompt user for the plaintext string
- printf("plaintext: ");
- string s = get_string();
- // Transform plaintext string by size 'k'
- printf("ciphertext: ");
- for (int i = 0, n = strlen(s); i < n; i++)
- {
- if (isupper(s[i]))
- {
- printf("%c", (((s[i] + k) - 65) % 26) + 65);
- }
- else if (islower(s[i]))
- {
- printf("%c", (((s[i] + k) - 97) % 26) + 97);
- }
- else
- {
- printf("%c", s[i]);
- }
- }
- }
- printf("\n");
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement