Created
June 1, 2016 02:45
-
-
Save CraigRodrigues/5b4fd2e2d29c9a2746d566fd589431ea to your computer and use it in GitHub Desktop.
My solution to CS50 pset2 - "Parlez-vous français?"
This file contains hidden or 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 <string.h> | |
#include <ctype.h> | |
/** | |
* Vigenere.c | |
* | |
* A program that encrypts messages using Vigenère’s cipher. This program | |
* must accept a single command-line argument: a keyword, k, composed entirely | |
* of alphabetical characters. If your program is executed without any | |
* command-line arguments, with more than one command-line argument, or with one | |
* command-line argument that contains any non-alphabetical character, your | |
* program should complain and exit immediately, with main returning 1 | |
* (thereby signifying an error that our own tests can detect). Otherwise, your | |
* program must proceed to prompt the user for a string of plaintext, p, which | |
* it must then encrypt according to Vigenère’s cipher with k, ultimately | |
* printing the result and exiting, with main returning 0. | |
* | |
* */ | |
int main(int argc, string argv[]) | |
{ | |
// check for 2 arguments only | |
if (argc != 2) | |
{ | |
printf("Nope\n"); | |
return 1; | |
} | |
// check if argument is all alpha char (no punct) - use loop and isalpha | |
for (int i = 0; i < strlen(argv[1]); i++) | |
{ | |
if (isalpha(argv[1][i]) == 0) | |
{ | |
printf("Nope\n"); | |
return 1; | |
} | |
} | |
// prompt user for codeword | |
string codeword = GetString(); | |
int j = 0; | |
// loop through the codeword. If not a letter than print unmodified. | |
for (int i = 0, n = strlen(codeword); i < n; i++) | |
{ | |
// to keep looping through the key continously | |
j = j % strlen(argv[1]); | |
// check if the char is alpha | |
if (isalpha(codeword[i])) | |
{ | |
// only 4 types of outcomes | |
if (islower(codeword[i]) && islower(argv[1][j])) | |
printf("%c", (((codeword[i] - 97) + (argv[1][j] - 97)) % 26) + 97); | |
else if (isupper(codeword[i]) && islower(argv[1][j])) | |
printf("%c", (((codeword[i] - 65) + (argv[1][j] - 97)) % 26) + 65); | |
else if (islower(codeword[i]) && isupper(argv[1][j])) | |
printf("%c", (((codeword[i] - 97) + (argv[1][j] - 65)) % 26) + 97); | |
else if (isupper(codeword[i]) && isupper(argv[1][j])) | |
printf("%c", (((codeword[i] - 65) + (argv[1][j] - 65)) % 26) + 65); | |
j++; | |
} | |
else | |
{ | |
printf("%c", codeword[i]); | |
} | |
} | |
printf("\n"); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
j = j % strlen(argv[1]);
This is where he modifies the length of J to the length of argv. Once J hits the length of argv it will re-start back over at the first char.
If this wasn't there, j would continue to increase with every char past the strlen of argv. This is a limiter and so needs to be reset prior to going into the if/else if functions.