Jul 19, 2012 at 10:52pm Jul 19, 2012 at 10:52pm UTC
It's because the % operator won't make a negative # positive so u'll have to add 26 more if the (..) is negative.
-13 % 26 == -13
13 % 26 == 13
Last edited on Jul 19, 2012 at 10:53pm Jul 19, 2012 at 10:53pm UTC
Jul 19, 2012 at 11:00pm Jul 19, 2012 at 11:00pm UTC
Line 30, lets see what happens if key is 42 and src[i] is 'e' (assuming ASCII is used).
1 2 3 4 5 6 7
src[i] = (char )(((src[i] - key - 'a' + 26) % 26) + 'a' );
src[i] = (char )((('e' - 42 - 'a' + 26) % 26) + 'a' );
src[i] = (char )(((101 - 42 - 97 + 26) % 26) + 97);
src[i] = (char )((-12 % 26) + 97);
src[i] = (char )(-12 + 97);
src[i] = (char )(85);
src[i] = 'U' ;
As you see the problem is that operator% does not work as you expect on negative values.
Last edited on Jul 19, 2012 at 11:04pm Jul 19, 2012 at 11:04pm UTC
Jul 19, 2012 at 11:07pm Jul 19, 2012 at 11:07pm UTC
Something is wrong with decrypt. However, if you can encrypt then you can decrypt. Something like this:
1 2 3 4
void caesar_decrypt(char src[100], int key, int dist)
{
caesar_encrypt(src, 26 - (key % 26), dist);
}
Encrypt a key of 26 is the same as decrypting a key of 0
Last edited on Jul 19, 2012 at 11:08pm Jul 19, 2012 at 11:08pm UTC
Jul 20, 2012 at 12:51am Jul 20, 2012 at 12:51am UTC
Topdog2904, thanks for your help. I changed the code and it worked perfectly. Thanks again.
Jul 26, 2012 at 6:18am Jul 26, 2012 at 6:18am UTC
thank you so much this helped me a lot!!