#include <cs50.h>
#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

char cycle(char p, int k)
{
//    printf("%i",k);
    int c = p + k;
    if (p <= 'Z') //upper case
    {
        c = 'A' + ((c - 'A') % 26 );
    }
    else //lower case
    {
        c = 'a' + ((c - 'a') % 26 );
    }
    return c;
}

int main(int argc, string argv[])
{
    if (argc != 2)
    {
        printf("wrong number of arguments!\n");
        return 1;
    }
    int k = atoi(argv[1]);
    printf("argc: %i,  argv0:%s, k:%i\n", argc, argv[0], k);
    string plain = get_string("plaintext: ");
    printf("ciphertext:");
    for (int i = 0, n = strlen(plain); i < n; i++)
    {
        if (isalpha(plain[i]))
        {
            printf("%c", cycle(plain[i],k));
        }
        else
        {
            printf("%c", plain[i]);
        }
    }
    printf("\n");
    return 0;
}