0

I'm new to C# and writing a text moving cipher program. However, when trying to set an array value to a value from another array (plus 3) i always get an IndexOutOfRangeException. That might sound confusing but honestly I have no clue how to really word this.

Removing the +3 didn't help, and it seems trying to set the array from another array always results in the error.

for (int i = 0; i < CipherLength; i++)
{
    if (Alphabet.Contains(CipherArray[i]))
    {
        Console.WriteLine(i);
        Console.WriteLine(CipherArray[i]);
        CipherArray[i] = Alphabet[CipherArray[i + 3]];
    }
    else
    {
        CipherArray[i] = ' ';
    }
}

Essentially, i'm trying to set the CipherArray value, in this case the character, to be that character moved by 3, which is what the alphabet array is for.

Expected: If CipherArray[i] = A, then after that it should equal D
Actual:

System.IndexOutOfRangeException: 'Index was outside the bounds of the array.'

(In all cases)

joanis
  • 10,635
  • 14
  • 30
  • 40
malton33
  • 113
  • 4
  • the `+ 3` is for `CipherArray` memory slot i + 3. It's not adding 3 to the value, it's the i+3th slot. it seems it's not there. Compare it with 5 boxes; you are trying to look in the 6th one. – Stefan Jul 07 '19 at 10:07
  • Don't think it is a duplicate, because this is about a specific problem and not just general IndexOutOfRangeException. – Gaurav Mall Jul 07 '19 at 10:32
  • 1
    @GauravMall, are you trying to say the question here is not about IndexOutOfRangeException, despite specifically asking about an IndexOutOfRangeException? The meaning of your comment unfortunately eludes me... –  Jul 07 '19 at 10:54
  • What I meant was that IndexOutOfRangeException happen because of a specific reason. The link you provided gives us the general idea of what and why IndexOutOfRangeExceptions happen, not why this specific implementation causes that error. Try to understand and do not be eluded. – Gaurav Mall Jul 07 '19 at 10:59
  • 1
    @GauravMall, that does not turn it into a non-duplicate question. The question is still about an IOORE. It seems to me that your comments express a belief that the question is not about IOORE just because the IOORE happened for some reason. Any exception always happens for a reason. That simple obvious fact does not make this question a non-duplicate. I still don't understand what you are trying to get at. –  Jul 07 '19 at 11:03
  • Neither do I understand where you want to get at. If you know why don't you just tell the error with this programming then? – Gaurav Mall Jul 07 '19 at 11:07
  • @GauravMall, your comment is incomprehensible. I flagged the question as a duplicate. Why do you demand from me to "_just tell the error with this programming then?_" What is this all about here? Perhaps it would be a good idea to take a breather... –  Jul 07 '19 at 11:08
  • I wasn't intending to offend you. I just said that I think this isn't necessarily a complete duplicate. I remark I did think. You just started asking questions all of a sudden. – Gaurav Mall Jul 07 '19 at 11:11
  • 1
    Okay, i see. I guess we were misunderstanding each other then. An IOORE is a very specific exception, it is not some vague error which changes its meaning or nature from case to case, and thus would have different explanations from case to case. An IOORE _always_ denotes the same specific problem (or a few handful of variations of the same problem, which the accepted answer in that question addresses). There is nothing "general" or "abstract" about an IOORE that would require it to be understood differently in different cases. (1/2) –  Jul 07 '19 at 11:18
  • My 2 cents, this question code is trying to implement a Ceasar Cipher solution, which is a key element to answering this question not merely an IndexOutOfRangeException... – mnemonic Jul 07 '19 at 11:19
  • (2/2) You might think otherwise, and that's okay. Nobody will ask you or force you to vote this question as a duplicate. This is a decision each SO user does on their own individually to the best of their knowledge... –  Jul 07 '19 at 11:26
  • When found in user code, exceptions like `NullReferenceException`, `IndexOutOfRangeException`, `ArgumentNullException`, and `ArgumentOutOfRangeException` are always some variation of the same basic bug. That's why we have canonical Q&A which explain how you can debug your code and fix the problem. You will, at the very least, be expected to have reviewed these posts, applied the knowledge provided within them to your own problem, and then explain in your question why those techniques did not apply in your own case, as well as provide a good [mcve] that reliably reproduces the problem. ... – Peter Duniho Jul 07 '19 at 17:05
  • ... You appear to have done none of these things, and certainly not the observable things we would see in your post. If you still feel your question is unique enough to be deserving to be answered separately from the marked duplicate, please improve your post so that it's clear why that's the case. – Peter Duniho Jul 07 '19 at 17:06

2 Answers2

3

This is because you are trying to access i + 3 element of CipgerArray, when i points to last element in an array, i + 3 is out of bounds.

Also you are complicating this. Generally characters are represented by ints:

  var ch = 'a';
  var i = (int)ch;
  // i = 97
  ch = 'z';
  i = (int)ch;
  // i = 122

So range is 26 characters. If you want to move every characters by 3, then you need to apply mod function, represented in C# by % operator (if you want to move from z, you need to get a c).

Putting it all together you can write your algorithm like this:

  var offset = 3;
  var toCipher = "abxz";
  toCipher = new string(toCipher
    .Select(ch => ((ch - 97) + offset) % 26 + 97)
    .Select(ch => (char)ch).ToArray());

Note that I didn't take into account uppercase letters.

Michał Turczyn
  • 32,028
  • 14
  • 47
  • 69
0

The problem is that when you get the

Alphabet[CipherArray[i + 3]]

you are passing it to the alphabet. I'm presuming that the cipher array has letters in it so passing it in the Alphabet will result in an error. What you need to do is say:

Alphabet[i + 3]

That's how I understood the problem. But that, of course, wouldn't fix it, what you really need to do is get the index of the letter in the Alphabet Array then add 3 to it. So:

Alphabet[Arrays.IndexOf(Alphabet, CipherArray[i]) + 3]

You will need to add in the alphabet array after the letter Z, A, B and C once more to compensate for the plus 3. If I did something wrong or didn't understand the problem tell me. Hope it helps.

Gaurav Mall
  • 2,372
  • 1
  • 17
  • 33