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>
- int main(int argc, string argv[]) {
- // Check to make sure only 1 thing is in the command-line
- if (argc != 2) {
- printf("Please enter in the command-line a string of alphabetical characters to compose your key.\n");
- return 1;
- }
- // Get the plaintext
- printf("plaintext: ");
- string plainText = get_string();
- // Take the key convert to string and make var j which will cycle through the key
- string key = argv[1];
- int j = 0;
- if (plainText != NULL) {
- printf("ciphertext: ");
- // Cycle through the plaintext that the user inputed
- for (int i = 0, n = strlen(plainText); i < n; i++) {
- // If it's a letter
- if (isalpha(plainText[i])) {
- // Length of pText % of the length of the key so once it maxes out the key will reset
- if (strlen(plainText) % strlen(key) == strlen(key)) {
- j = 0;
- }
- // Lowerase letters first
- if (islower(plainText[i])) {
- char lower = ((((plainText[i]) - 97) + key[j]) % 26) + 97;
- printf("%c", lower);
- j++;
- }
- // Uppercase letters
- else if (isupper(plainText[i])) {
- char upper = ((((plainText[i]) - 65) + key[j]) % 26) + 65;
- printf("%c", upper);
- j++;
- // Spaces/punctuation
- } else {
- printf("%c", plainText[i]);
- }
- }
- }
- // Make things look clean and return 0 to show things are all good
- printf("\n");
- return 0;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement