Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <cs50.h>
- #include <stdio.h>
- #include <string.h>
- #include <stdlib.h>
- #include <ctype.h>
- // take in command line argument & eliminate any wrong intake (lines 12-14)
- int main(int argc, string argv[]) {
- if (argc != 2){
- printf ("mising command-line arguement\n");
- return 1;
- } else {
- // check if argc[1] is a string
- string key = (argv[1]);
- int keylen = strlen(key);
- //inside the for loop;
- // 1) checking if the input is solely aplha
- // 2) changeing key to all lowercase
- for (int str = 0; str < keylen; str++){
- if (!isalpha(key[str])) {
- printf("Key needs to be only characters!\n");
- return 1;
- }
- if ((isupper(key[str]))) {
- key[str] = (tolower(key[str]));
- }
- }
- // now key is in lowercase
- // will have to use isalpha when iterating over plaintext
- //take in plaintext and display ciphertext;
- printf("Plaintext: ");
- string plaintext = get_string();
- printf("Ciphertext: ");
- // now to iterate over the plaintext
- int g = 0;
- for (int q = 0; q < strlen(plaintext) ; q++){
- //need to ensure that its only letters that change using ASCII values
- int l = (g % (strlen(argv[1])));
- if islower(plaintext[q]) {
- printf("%c", ((((plaintext[q] - 97) + (key[l] - 97)) %26 ) +97 ));
- g++;
- }
- else if isupper(plaintext[q]) {
- printf("%c", ((((plaintext[q] -65) + (key[l] - 65)) %26 ) +65 ));
- g++;
- }
- else {
- printf("%c", (plaintext[q]));
- }
- }
- printf("\n");
- }
- }
Add Comment
Please, Sign In to add comment