Created
April 17, 2014 00:52
-
-
Save mitrnsplt/10945731 to your computer and use it in GitHub Desktop.
A solution for cs50's Vigenere problem, a cipher that is a little more complicated than Caesar
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
/** | |
* vigenere.c | |
* | |
* Peter Downs | |
* | |
* A code for encrypting text with a Vigenere cipher. | |
*/ | |
#include <cs50.h> | |
#include <stdio.h> | |
#include <stdlib.h> | |
#include <string.h> | |
#include <ctype.h> | |
int main(int argc, string argv[]) | |
{ | |
// Get the key to the cipher | |
string key = argv[1]; | |
if (argc != 2) | |
{ | |
printf("Please give me a valid key\n"); | |
return 1; | |
} | |
else | |
{ | |
//Check that it is a letter key | |
for (int i = 0, n = strlen(key); i < n; i++) | |
{ | |
if (isalpha(key[i])) | |
{ | |
} | |
else | |
{ printf("Invalid key. Try again\n"); | |
return 1; | |
} | |
} | |
} | |
//Get the text to encrypt | |
string plntxt = GetString(); | |
if (plntxt != NULL) | |
{ | |
//Encrypt and print | |
for (int i = 0, j = 0, n = strlen(plntxt); i < n; i++, j++) | |
{ | |
if (j > strlen(key) - 1) | |
{ | |
j = 0; | |
} | |
int c = 0; | |
//Check case of textual character | |
//Assign it an alphabet number and encrypt | |
if (isupper(plntxt[i])) | |
{ | |
c = (((plntxt[i] - 65) + (tolower(key[j]) - 97))%26) + 65; | |
printf("%c", (char)c); | |
} | |
else if (islower(plntxt[i])) | |
{ | |
c = (((plntxt[i] - 97) + (tolower(key[j]) - 97))%26) + 97; | |
printf("%c", (char)c); | |
} | |
else | |
{ | |
printf("%c", plntxt[i]); | |
j--; | |
} | |
} | |
} | |
printf("\n"); | |
return 0; | |
} | |
can i ask you?
this is not the correct solution...
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
thanks!