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>
- bool only_digits(string s);
- int rotate(string text, int k);
- int atoi(string t);
- int main(int argc, string argv[])
- {
- //Command line gets a user input of an int, error code if other input given than a single arg
- if (argc != 2)
- {
- printf("Usage: ./caesar key\n");
- return 1;
- }
- //Check if digits
- if (only_digits(argv[1]) == false)
- {
- printf("Usage: ./caesar key\n");
- return 1;
- }
- //else
- //{
- //printf("Works! \n");
- //}
- int key;
- key = atoi(argv[1]);
- //Program prompts user for text input
- string ptext = get_string("Plaintext: \n");
- //Run function that uses text input as array and replaces letters by a number of positions (int from command line) in alphabet array
- rotate(ptext, key);
- //print new coded message
- }
- bool only_digits(string s)
- {
- int digits = 0;
- int n = strlen(s);
- for (int i = 0; i < n; i++)
- {
- if (isdigit(s[i]))
- {
- digits++;
- }
- }
- if (digits == n)
- {
- return true;
- }
- else
- {
- return false;
- }
- }
- int rotate(string text, int k)
- {
- printf("ciphertext: ");
- for (int i = 0, n = strlen(text); i < n; i++)
- {
- if (!isalpha(text[i]))
- {
- printf("%c", text[i]);
- }
- else if (isupper(text[i]))
- {
- printf("%c", ((((text[i] - 65) + k) % 26) + 65));
- }
- else if (islower(text[i]))
- {
- printf("%c", ((((text[i] - 97) + k) % 26) + 97));
- }
- else
- {
- printf("Something went wrong.");
- }
- }
- printf("\n");
- return 0;
- }
Add Comment
Please, Sign In to add comment