0

I was able to complete my code and it works when I "make Vigenere" however for some reason, it is outputting double the amount of letters than the plaintext. I figure it has something to do with my loop however I canĀ“t figure it out. I would appreciate any assistance. I am attaching only the last part of my code associated with this. Thanks

 //get key 
    string key = argv[1];

    // get plaintext from user and save as plaintext variable
    {
    printf("plaintext:");
    }
    string plaintext = get_string();

    // iterate over characters in string, check upper and lower case and encrypt

    for (int i=0, j=0, n = strlen(plaintext); i < n; i++)
{   
    {
        if (isupper(plaintext[i]) && isupper(key[j]))
        {
        printf("%c", (((plaintext[i]-65) + (key[j] - 65)) % 26) + 65);        
        }

        else if (islower(plaintext[i]) && islower(key[j]))
        {
        printf("%c", (((plaintext[i]-97) + (key[j]-97)) % 26) + 97);
        }

        else if (islower(plaintext[i]) && isupper(key[j]))
        {
        printf("%c", (((plaintext[i] - 97) + (key[j] -65)) % 26) + 97);
        }

        else if (isupper(plaintext[i]) && islower(key[j]))
        {
        printf("%c", (((plaintext[i] - 65) + (key[j] - 97)) % 26) + 65);
        }
        j++;
    } 

     {
        printf("%c", plaintext[i]);
    }   
}
    printf("\n");

return 0;
}
0

The code has the large block of code that encodes the alphas, but then it is followed by the last printf statement which prints the original char whether it is an alpha or not. Seems like you might want to think about adding an isalpha check followed by an else statement. I'll let you figure out the details.

BTW, What happens when j > strlen(key)?

If this answers your question, please click on the check mark to accept. Let's keep up on forum maintenance. ;-)

| improve this answer | |
  • Thanks but struggling to solve problem. I have removed the printf at end of code to print the plaintxt and added statement ( j = j % strlen(key); to have key wrap arnd if it is shorter than the plaintext however now I am getting the following errors which I cannot get rid of - any assistance would be much appreciated: error: expected ';' in 'for' statement specifier; error: use of undeclared identifier 'j'; error: use of undeclared identifier 'n'; error: use of undeclared identifier 'i'. Also, why is it necessary to do an isalpha check if this was done in first part of problem? thanks – antonio.caminiti May 15 '17 at 11:20
  • I just figured out why I need the ifalpha! (eg. for spaces). Would still appreciate assistance as to why errors. thanks – antonio.caminiti May 15 '17 at 11:39
  • Sounds like a typo or malformation of the for statement. Without seeing it, there's no way to say what's wrong. – Cliff B May 15 '17 at 19:52

You must log in to answer this question.

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