# Thread: Dealing with the end of an Array

1. ## Dealing with the end of an Array

k is an int between 0 and 25 (letters of the alphabet).

Code:
```in a for loop that increments i

int temp = message[i];
temp = temp+k;
message[i] = temp;```
What I'm doing is a letter shift. A=0, B=1, C=2, etc. If k is 0, message[i] remains the same. if k is 1, message[i] is moved from letter 1 (lets say A) to letter 2 (B).

Here is a hypothetical situation to illustrate my problem:

k = 25
message[i] = Z

ASCII value of 90 + 25 = 115, a lower case "s".

What I'd do is print out all Upper case letters. So if message[i] reaches 90 it returns back to 65 (A) and increments from there. (In this example that would result in Z translating to 85, the letter U).

Any ideas?

2. I'm confused about exactly what's happening in this program, but:

>>So if message[i] reaches 90 it returns back to 65 (A) and increments from there.
You should consider using 'Z' and 'A' rather than 90 and 65, as they are much easier to understand. As for the problem of having it 'wrap', consider using the modulus (%) operator.

3. I was using Z and A to begin with...it really confused me.
I then put in ASCII values and it worked out a lot better. I might not have been perfectly clear in explaining the problem

all of them are inputted as Z/A/whatever letter, but then changed to ASCII for the inner workings of the program (its easier to make an A a B if all you have to do is add 1)

modulous operator? Drat! I always forget that its at my disposal :P (I guess I just think of the basic functions on the calculator [*, +, -, =, etc])
Thanks! *goes back to program to see what happens*

4. >>but then changed to ASCII for the inner workings of the program
You should remember though, all characters are stored as ASCII anyways A 'char' is just an int that holds values from -128 to 127, and you can do +1 -1 etc. as you would to any other integral datatype.

Have fun with modulus

5. Doh! You're right! :P

Hmm...I can't seem to figure it out.
I did a google search on wrapping an array with modulus and couldn't find anything to help either.

Gunna think out loud (or in text I guess..) here...

So I need anything greater then Z (90) back to A (65) and then continue on....

while ( temp > 90) { // something over Z
// wrap back to 65 // go back to A, 26 letters in alphabet
}
OK, I put in a temp = (temp-26); and that seemed to work...

But you've gotten me curious now, how could it be done with modulus?

6. Z%26 will always be between 0 and 25 and will loop back to 0 when it hits 26. Add that to 65 and there ya go

7. Yes, try something like:
wrappedValue = ((value - 'A') % ('Z' - 'A' + 1)) + 'A'

First, bring 'A' to be represented by 0, 'B' by 1, etc. (value - 'A') so that it can be used with modulus. Then, wrap it with modulus (% by number of characters in alphabet), and convert it back to ordinary ASCII (+ 'A'), and you have your answer!

Note that since it's common knowledge that the alphabet contains 26 characters, you could replace ('Z' - 'A' + 1) with (26). But I just felt like demonstrating the power of ' '

8. Ah, I get it! Thanks guys!