Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #import <stdio.h>
- #import <cs50.h>
- #include <stdlib.h>
- #include <string.h>
- #include <ctype.h>
- int main (int argc, string argv[])
- int i;
- {
- //checking if te input has 2 arguments
- if (argc != 2 ) { printf("try to use 2 arguments\n"); return 1;
- }
- //convert argv to string "key"
- string key = argv[1];
- if (argc == 2)
- {
- //plaintext input
- string text = get_string("Enter plaintext: ");
- printf("ciphertext: ");
- // print out the plaintext letter by letter
- for (int i =0, n = strlen(text); i < n ; i++)
- // here is where I struggle, the sub script to wraparound the key only if its an alphabetic character.
- if isalpha(text[i]) {
- for (int keyindex = 0, nk = strlen(key); keyindex < nk; keyindex++)
- keyindex % nk;
- }
- // convert the plaintext to ciphertext
- if islower(text[i])
- printf("%c", (((text[i] + key[keyindex]) - 97) % 26) + 97);
- else if isupper(text[i])
- printf("%c", (((text[i] + key[keyindex]) - 65) % 26) + 65);
- else printf("%c",text[i]);
- printf("\n");
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement