#include <stdio.h>
#include <cs50.h>
#include <stdlib.h>
#include <ctype.h>
#include <string.h>
int main(int argc, string argv[])
{
int alph_indx(char ch);
if (argc != 2)
{
printf("usage ./vigenere keyword, where k is non negative integer");
return 1;
}
string k = argv[1];
printf("plaintext: ");
string plaintext = get_string();
string ciphertext = plaintext;
//encipher
for (int i = 0, n = 0; i < sizeof(plaintext); i++)
{
if (isalpha(plaintext[i]))
{
if (isupper(plaintext[i]))
{
//toupper
ciphertext[i] = ( alph_indx(plaintext[i]) + alph_indx(k[n % strlen(k)]) ) + 'A';
}
else if (islower(plaintext[i]))
{
//tolower
ciphertext[i] = ( alph_indx(plaintext[i]) + alph_indx(k[n % strlen(k)]) ) + 'a';
}
else if (isblank(plaintext[i]))
{
ciphertext[i] = plaintext[i];
}
}
}
printf("ciphertext: %s", ciphertext);
return 0;
}
int alph_indx(char ch)
{
int num = ch;
return (isupper(ch)) ? ((num - 'A') % 26) : ((num - 'a') % 26);
}
I2luY2x1ZGUgPHN0ZGlvLmg+CiNpbmNsdWRlIDxjczUwLmg+CiNpbmNsdWRlIDxzdGRsaWIuaD4KI2luY2x1ZGUgPGN0eXBlLmg+CiNpbmNsdWRlIDxzdHJpbmcuaD4KCmludCBtYWluKGludCBhcmdjLCBzdHJpbmcgYXJndltdKQp7CiAgICBpbnQgYWxwaF9pbmR4KGNoYXIgY2gpOwogICAgaWYgKGFyZ2MgIT0gMikKICAgIHsKICAgICAgICBwcmludGYoInVzYWdlIC4vdmlnZW5lcmUga2V5d29yZCwgd2hlcmUgayBpcyBub24gbmVnYXRpdmUgaW50ZWdlciIpOwogICAgICAgIHJldHVybiAxOwogICAgfQogICAgc3RyaW5nIGsgPSBhcmd2WzFdOwogICAgcHJpbnRmKCJwbGFpbnRleHQ6ICIpOwogICAgc3RyaW5nIHBsYWludGV4dCA9IGdldF9zdHJpbmcoKTsKICAgIHN0cmluZyBjaXBoZXJ0ZXh0ID0gcGxhaW50ZXh0OwogICAgLy9lbmNpcGhlcgogICAgZm9yIChpbnQgaSA9IDAsIG4gPSAwOyBpIDwgc2l6ZW9mKHBsYWludGV4dCk7IGkrKykKICAgIHsKICAgICAgICBpZiAoaXNhbHBoYShwbGFpbnRleHRbaV0pKQogICAgICAgIHsKICAgICAgICAgICAgaWYgKGlzdXBwZXIocGxhaW50ZXh0W2ldKSkKICAgICAgICAgICAgewogICAgICAgICAgICAgICAgLy90b3VwcGVyCiAgICAgICAgICAgICAgICBjaXBoZXJ0ZXh0W2ldID0gKCBhbHBoX2luZHgocGxhaW50ZXh0W2ldKSArIGFscGhfaW5keChrW24gJSBzdHJsZW4oayldKSApICsgJ0EnOwogICAgICAgICAgICB9CiAgICAgICAgICAgIGVsc2UgaWYgKGlzbG93ZXIocGxhaW50ZXh0W2ldKSkKICAgICAgICAgICAgewogICAgICAgICAgICAgICAgLy90b2xvd2VyCiAgICAgICAgICAgICAgICBjaXBoZXJ0ZXh0W2ldID0gKCBhbHBoX2luZHgocGxhaW50ZXh0W2ldKSArIGFscGhfaW5keChrW24gJSBzdHJsZW4oayldKSApICsgJ2EnOwoKICAgICAgICAgICAgfQogICAgICAgICAgICBlbHNlIGlmIChpc2JsYW5rKHBsYWludGV4dFtpXSkpCiAgICAgICAgICAgIHsKICAgICAgICAgICAgICAgIGNpcGhlcnRleHRbaV0gPSBwbGFpbnRleHRbaV07CiAgICAgICAgICAgIH0KICAgICAgICB9CiAgICB9CiAgICAKICAgIHByaW50ZigiY2lwaGVydGV4dDogJXMiLCBjaXBoZXJ0ZXh0KTsKICAgIAogICAgcmV0dXJuIDA7CiAgICAKfQppbnQgYWxwaF9pbmR4KGNoYXIgY2gpCnsKICAgIGludCBudW0gPSBjaDsKICAgIAogICAgcmV0dXJuIChpc3VwcGVyKGNoKSkgPyAoKG51bSAtICdBJykgJSAyNikgOiAoKG51bSAtICdhJykgJSAyNik7Cn0=