-
-
Save YorikoHashida/a0595ef70d56a277134460aef00b3725 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <cs50.h> | |
#include <stdlib.h> | |
#include <string.h> | |
#include <ctype.h> | |
int main (int argc, string argv[]) { | |
// check if you type a single command-line argument | |
if (argc != 2) { | |
printf ("Type ./vigenere keyword"); | |
return 1; | |
} | |
else { | |
string keyword = argv[1]; | |
// check if the keyword is composed entirely of alphabetical characters | |
int count = 0; | |
for (int h = 0; h < strlen(keyword); h++) { | |
if (isalpha(keyword[h]) == 0) count++; | |
} | |
if (count > 0) { | |
printf ("Type alphabetical characters."); | |
return 1; | |
} | |
else { | |
// now get the plaintext | |
string s = GetString(); | |
int j = 0; | |
for (int i = 0; i< strlen(s); i++) { | |
j = j % strlen(keyword); | |
// convert keyword[j] to 0-25 | |
if (65 <= keyword[j] && keyword[j] <= 90) { | |
keyword[j] = keyword[j] - 65; | |
} | |
else if (97 <= keyword[j] && keyword[j] <= 122) { | |
keyword[j] = keyword[j] - 97; | |
} | |
// now encrypt the plaintext! | |
if (65 <= s[i] && s[i] <= 90) { | |
// convert ASCII code to 0-25 (LARGE LETTERS) | |
char letter = s[i] - 65; | |
letter = (letter + keyword[j]) % 26; | |
// reconvert | |
letter = letter + 65; | |
printf ("%c", letter); | |
j++; | |
} | |
else if (97 <= s[i] && s[i] <= 122) { | |
// convert ASCII code to 0-25 (small letters) | |
char letter = s[i] - 97; | |
letter = (letter + keyword[j]) % 26; | |
// reconvert | |
letter = letter + 97; | |
printf("%c", letter); | |
j++; | |
} | |
else { | |
printf ("%c", s[i]); | |
} | |
} | |
printf ("\n"); | |
return 0; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment