Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * pset 2
- * Caesar's Cipher
- *
- * Mike
- *
- * Scrambles a message by k characters
- */
- #include <stdio.h>
- #include <cs50.h>
- #include <stdlib.h>
- #include <string.h>
- #include <ctype.h>
- int main(int argc, string argv[])
- {
- if(argc != 2 || atoi(argv[1]) < 1)
- {
- printf("error\n");
- return 1;
- }
- // get k
- int k = atoi(argv[1]);
- int i;
- // get message
- string scramble = GetString();
- // scramble and output
- for(i = 0; i < strlen(scramble); i++)
- {
- //lowercase
- if(isupper(scramble[i]))
- {
- int code = (scramble[i] - 65 + k) %26;
- printf("%c", (char) code + 65);
- }
- //uppercase
- else if(islower(scramble[i]))
- {
- int code = (scramble[i] - 97 + k) %26;
- printf("%c", (char) code + 97);
- }
- else (printf("%c", scramble[i]));
- }
- printf("\n");
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement