0

so my vigenere cipher seems to work for uppercase letters but for some lowercase letters, it returns � for some reason. The code for lowercase and uppercase is structurally similar therefore I don't know why this is happening so I would appreciate if anyone has any input on this.

[code snipped per Honor Code. In the future, please describe using pseudocode.]

2

What you are finding is a lesser known issue. A char behaves numerically like an 8-bit signed integer. When a char's numeric value gets to 128 or more, the first, most significant bit becomes a 1 and the system treats it like a negative number in two's complement form. Once you assign a value of 128 or more to a char, as you did with temp = temp + shifted[wrap%klength];, it goes negative, but you are still treating it as a positive. So, the result is not what you expect.

If the combination of a lower case letter and the shift key is large enough, you can easily exceed 127.

You need to modify your code to make sure that the value of any variable defined as a char never exceeds 127, or that you deal with it when it does.

| improve this answer | |
  • God bless you sir! I've been cracking my head over this for 3 days. I realized something was wrong when with a key of 'z', it works up to f, then it returns the question mark. After reading this, all I did was modify " char temp " into " int temp ", and it passed check50. Is it because an int is 2 bytes compared to a char which is 1 byte? Then if the number is larger that 256, will it face the same problem? – JaiPaw May 5 '15 at 1:57
  • With two bytes, that gives you 15 bits of number and 1 sign bit. I believe that means the range is from -32768 to +32767. Glad I could help. – Cliff B May 5 '15 at 2:05
0

Have you done the caesar's problem, that's should be you're base code as you will just need to modify it accepting instead of a key number, it would be a value from 0..26 where 0 is A.

And as I've looked in to you're code you didn't wrap it in modulo 26. The modulo does wrap you're number in a specific limit or in other mathematical word it will return the remainder of the division of two points. For example 13 % 12 is equal to 1, and 1 % 12 is equal to 1, and so on etc. And also you didn't use the formula of caesar to easily manipulate the code base.

Where the formula for caesar and vigenere is:

c = (j + k) % 26

You should check the instruction video that is included in the problem set, the answer is all there where Zamyla explain. She also showed a small snippet of code there.

| improve this answer | |
  • That wasn't a deal breaker or the main problem though. But thanks for reminding me about that formula, it might be able to simplify and improve my code. :) – JaiPaw May 5 '15 at 1:59

You must log in to answer this question.

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