Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //caesar_character is a function that takes a single character and the key to use for the cypher and returns the encoded character back.
- #include <cs50.h>
- #include <ctype.h>
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- char caesar_character(char ch, int key);
- int main(int argc, string argv[])
- {
- if (argc != 2)
- {
- printf("You must suply exactly one argument!\n");
- return 1;
- }
- int key = atoi(argv[1]);
- string original = GetString();
- for (int i = 0, len = strlen(original); i < len; ++i)
- {
- printf("%c", caesar_character(original[i], key));
- }
- printf("\n");
- return 0;
- }
- char caesar_character(char ch, int key)
- {
- if (islower(ch))
- {
- return (ch - 'a' + key) % 26 + 'a';
- }
- else if (isupper(ch))
- {
- return (ch - 'A' + key) % 26 + 'A';
- }
- else
- {
- return ch;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement