Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <cs50.h>
- #include <string.h>
- #include <ctype.h>
- // Code techniques not taught in CS50 are from previous programming experience
- int main(int argc, string argv[])
- {
- // If the user passes the correct amount of arguments...
- if (argc == 2)
- {
- // Convert the user's argument into an int named argPassed
- int argPassed = atoi(argv[1]);
- // ...receive input for string plainText
- printf("plaintext: ");
- string plainText = get_string();
- printf("ciphertext: ");
- // Constrain the key to 26 value
- argPassed = argPassed % 26;
- // ...for each character in the string typedName...
- for (int i = 0; i < strlen(plainText); i++)
- {
- // ...create char variable "plainChar" with that chararcter
- char plainChar = plainText[i];
- // ...if the character is not a letter...
- if (plainChar > 'z' || plainChar < 'A')
- {
- // ...print that character
- printf("%c", plainChar);
- }
- // ...if character + argument <= z/Z and >= a/A...
- else if ((plainChar + argPassed <= 'z' && plainChar + argPassed >= 'a') || (plainChar + argPassed <= 'Z' && plainChar + argPassed >= 'A'))
- {
- // ...print the user's input + argument
- printf("%c", plainChar + argPassed);
- }
- // ...if character + argument > z or > Z but < a
- else if ((plainChar + argPassed > 'z') || (plainChar + argPassed > 'Z' && plainChar + argPassed < 'a'))
- {
- printf("%c", plainChar + argPassed - 26);
- }
- }
- // End on a new line
- printf("\n");
- }
- else
- {
- // Print an error and return 1 if the user enters an incorrect amount of arguments
- printf("Please enter an argument in the form of a single integer.\n");
- return(1);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement