1

I do not know why but when I run my program with k = 6 and I type z I get the weird character �

Here is my code:

s[i] = s[i] + k;  
if (s[i] > 122)  
s[i] = (s[i] % 26 + 78);

example with 5 as k

(~/Dropbox/pset2): ./caesar 5  
z  
e

example with 6 as k

(~/Dropbox/pset2): ./caesar 6  
z  
�  

I have made a test program to check this and it gives the correct ASCII code for the letter f (102).

int y = 128 % 26 + 78;  
printf("%i\n", y);

(~/Dropbox/pset2): ./test    
102

Thank you in advance!

2
  • Have you tried using printf to check if s[i] is what you think it is?
    – Quevun
    Mar 3 '15 at 0:18
  • Thank you, by doing this on every step helped me to figure out the problem. Mar 3 '15 at 18:19
1

Please remember that you are adding the ASCII codes. (a != 0).

My suggestion is to adjust the values of a-z to the range 0-25 BEFORE adding, then take the modulus of the sum. Adjust sum to ASCII and fix case.

1
  • Thank you, this is what I needed. Mar 3 '15 at 18:22
0

The problem was that char only goes up to 127, when I entered a value ok k = 6, z turned into 128 (122 for ASCII plus 6)and it was out of the scope, this caused s[i] to be - 128. So if this happens to you you need to adjust the values of a-z to the range 0-25 BEFORE adding as Strom said.

You must log in to answer this question.

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