0
int main(int argc, string argv[])
{
    int i,n,k;
    if(argc!=2)
    {
        printf("Enter correct number of command line arguments\n");
        return 1;
    }
    else
    {
        k=atoi(argv[1]);
    }
    printf("plaintext:");
    string p=get_string();
    n=strlen(p);
    printf("ciphertext:");
    for(i=0;i<n;i++)
    {
        int x=(p[i]+(k%26));
        if (p[i]>='a' && p[i]<='z')
        {
            if (x>'z')
            {
                x-='z';
                x+='a';
                printf("%c",x-1);
            }
            else
            {
                printf("%c",x);
            }

        else if(p[i]>='A' && p[i]<='Z')
        {
            if (x>'Z')
            {
                x-='Z';
                x+='A';
                printf("%c",x-1);
            }
            else
            {
                printf("%c",x);
            }
        }
        else 
        {
            printf("%c",p[i]);
        }
    }
    printf("\n");
    return 0;
}

1 Answer 1

1

Your code looks pretty solid :)! The only things I'd personally change is use islower(), isupper() and isalpha() (checking for lowercase, uppercase and weather or not it is an alpha numberical character) as they give you smaller statements in your ifs. Other than that the only thing that I'd personally do is use a few more white lines, spaces and possibly throw the encoding logic into a function (sothat it is reusable and doesn't clutter up main), but that has more to do with style! Hope this helps :)

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .