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[])
- {
- // checks if command line input is 2 or less
- if (argc != 2)
- {
- printf("Usage: ./caesar key\n");
- printf("1");
- return 1;
- }
- else if (argc == 2)
- {
- // check key for validity (positive int)
- // convert argv to int
- const int KEY = atoi(argv[1]);
- // store boolean
- bool KeyValid = true;
- // store key len
- int len = strlen(argv[1]);
- // Loop checking each digit
- for (int i = 0; i < len; i++)
- {
- // if isdigit (part of ctype.h) detects a non-digit turns bool to false
- if (isdigit(argv[1][i]) == false)
- {
- // if anything besides positive int is entered the program will print false and end
- KeyValid = false;
- printf("false\n");
- return 0;
- }
- }
- // if input is positive int, this condition will execute
- if (KeyValid == true)
- {
- // this will prompt user for input
- string plain = get_string("plaintext: ");
- // this determines the length of the user input
- int plainLength = strlen(plain);
- // print "ciphertext: " once before the iteration loop happens
- printf("ciphertext: ");
- // iterate i against the length of plain text entered, basically scanning every character
- for (int i = 0; i < plainLength; i++)
- {
- // turn plain[i] into a character variable, so it can be scanned individually and printed
- char p = plain[i];
- //int ci = (p + KEY) % 26;
- if (isalpha(p) == false)
- {
- printf("%c", plain[i]);
- }
- if (isupper(p) == true)
- {
- printf("%c", p + KEY);
- }
- else
- {
- printf("%c", p + KEY);
- }
- }
- printf("\n");
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement