I need to write a program that decrypts an array of strings. It has to do other things too, but I want to get this part working first. I don't have to check for uppercase or lowercase, all of the messages are in upper case. The problem is most likely in the Decipher function, but I thought I should include main too.
The contents of main are just to test my Decipher function right now. cipher is the array of strings to be decrypted, and I'm testing it with one key from the list right now because more will be needed later. The .h file is fully constructed and contains the constants like the cipher array, MAXLENGTH, and NUMMESSAGES. But the output of this is very strange.
I just changed Line 25 to that and while it does generate actual characters now, they're not the correctly deciphered message. Instead I'm getting this.
Note that the % operator doesn't work like modular arithmetic in mathematics. Instead it computes the remainder after division so if you use a negative value you will get a negative value back.
Note that the % operator doesn't work like modular arithmetic in mathematics. Instead it computes the remainder after division so if you use a negative value you will get a negative value back.
So you're saying I should have a check to see if ciph[i] - key is negative, then convert it to positive before using modulo? That makes sense, I'll try it out. Thanks!
I should have a check to see if ciph[i] - key is negative, then convert it to positive before using modulo?
It's not as simple as just flipping the sign.
A trick that I sometimes use is to add the right operand to the left operand. So instead of doing k % N I do (N + k) % N. This will compute the modulo value as a positive number correctly as long as k is greater than -N.
A trick that I sometimes use is to add the right operand to the left operand. So instead of doing k % N I do (N + k) % N. This will compute the modulo value as a positive number correctly as long as k is greater than -N.
I see, does that work for both positive and negative k? Or just for negative k?
Hi mkb555, I am currently working on a similar project as you. However mine includes another function SolveCipher( const char cip[] , char dec[] ). It is responsible for returning the key if the deciphered message begins with a crib, returning '\0' otherwise. If the key is non-zero, the deciphered message is returned in dec. I am confused as to how to go about this. The cribs are contained in a const char 2d array similar to the cipher array.
And to reaper, I can't really give you exact code, but I'll explain how I'd do it. I'd call the Decription function within SolveCipher then compare the deciphered messages to each of the cribs. As soon as it finds a match, return the deciphered message and the key.
reaper007, I believe the key is the only return value. Since you're passing an array to dec, just have the function change the value of dec to the deciphered code. Then when you output it, just output the array you passed to SolveCipher. If you want to output the key, just output the function itself since it returns the key.