0

So far, I've managed to make Vigenere Cipher work well, but my key always encounters a problem if it has uppercase character.

for (int i = 0, b = 0, n = strlen(phrase); i < n; i++, b++)
    {

    if (isalpha(phrase[i]))
    {

        shift = tolower(key[b % strlen(key)]) - 'a';

         cipher = phrase[i] + shift;

     }

Basically, shift is an integer that finds the integer equivalent of the current character in the keyword. Which I add to the current char in the phrase. Inside the formula for shift, its set to automatically make that specific char lowercase, to simplify the process, but it doesn't seem to work. If needed for clarification, I will post the rest of the for loop, but I think this is the main problem.

PS: Sorry if my code seems kind of confusing, or has bad style, I just want to make sure it works, before making it more efficient. Thanks so much in advance.

1

It seems that your analysis of the problem is a little off. I don't think it's an issue of the key being uppercase so much as it is other problems intersecting that condition.

I see a couple of issues in the posted code. First, look at the following line:

     cipher = phrase[i] + shift;

Say that phrase[i] = 'z' or 'Z'. What is the result if you add anything to it? If uppercase, it will simply add the shift value to 'Z' and, if shift > 6, produce a lower case letter when it should loop around to the beginning of the upper case alphabet.

Second, the code increments b for every char processed in phrase[]. The index for the key should only be incremented when an alpha is processed and never incremented when a non-alpha is incremented.

There may be other issues, but that would be hard to know from the limited amount of code posted.

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

1
  • Thanks for the help, I actually ended up removing cipher and adjusting some more things, and everything else seems to be working now. I'm just glad I eventually figured it out. Dec 27 '16 at 6:51

You must log in to answer this question.

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