#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);
}