-1

Hi just needed a little bit of help with a crash im having, so im putting together a little program that will work similar to a Caesar cipher by shifting the letters by a set value, however each subsequent letter is shifted to a different letter, rather then all letters being shifted by the same value (which is easy to crack with frequency analysis).

Anyway the program works fine if the word being encoded is shorter or the same length as the code (like a 7 letter word being encoded by a 7 digit code), but as soon as you try to encode a word longer than it, it crashes with an array out of bounds error or something.

What needs to happen is it needs to loop back around to the start of the code until no more letters need encoding.

```

```
static char ZipherEncode(char input, int shift)
{
    char letter = (char)(input + shift);

    if (letter > 'z')
    {
        letter = (char)(letter - 26);
    }
    else if (letter < 'a')
    {
        letter = (char)(letter + 26);
    }

    return letter;
}

static char ZipherDecode(char input, int shift)
{
    char letter = (char)(input - shift);

    if (letter > 'z')
    {
        letter = (char)(letter - 26);
    }
    else if (letter < 'a')
    {
        letter = (char)(letter + 26);
    }

    return letter;
}

int[] codeShift = new int[] { 4, 2, 6, 3, 0, 5, 1 };

Console.WriteLine("Please enter a word to encode: ");

string input = Console.ReadLine();

char[] zipherEncoded = new char[input.Length];

zipherEncoded = input.ToCharArray();



Console.WriteLine("\n\nENCODED: ");

int currCode = codeShift[0];
int counter = 0;

for (int i = 0; i < zipherEncoded.Length; i++)
{
    if (zipherEncoded[i] != '\0')
    {
        //DEBUG
        Console.WriteLine("WE ARE HERE!");


        Console.Write("{0}", ZipherEncode(zipherEncoded[i], codeShift[i]));
    }
    else
    {
        //DEBUG
        Console.WriteLine("WE ARE HERE (ELSE)!");

        Console.Write("{0}", ZipherEncode(zipherEncoded[i], codeShift[currCode]));
        //Console.Write("{0}", ZipherEncode(zipherEncoded[i], codeShift[counter]));
        //Console.Write("{0}", ZipherEncode(zipherEncoded[i], Array.IndexOf(codeShift, currCode)));
        counter++;
    }
}


Console.ReadLine();

```

```

The output when I encode the word "volcano" reads - ENCODED: zqrfasp

This is correct, however when I try and encode "volcanocat", because the word is longer than the 7 digits in the code it crashes, the output should read - ENCODED: zqrfaspgcz

the "cat" part of the phrase should be encoded with a shift of 426 <<< loops back to the start of the codeShift array storing our shifts.

C (Shift of 4) > G
A (Shift of 2) > C
T (Shift of 6) > Z

As from the parts that are commented out you can see im trying to find a way to have it loop back around to the start of the shift code to finish off the final letters.

Im really new to C# so there may be a function built in to do what I want, but I cant get my head around it!

I think I cant see the forrest through the trees!

Stiffeno
  • 3
  • 1
  • 1
    *"it crashes with an array out of bounds error or something"* A good start would be to read the error message, look up what it means, and search for other questions here that have the same message. Also, you should post the text if the error message in the question. – Rufus L Nov 23 '22 at 00:44
  • FYI, there's no point in `char[] zipherEncoded = new char[input.Length];` since you just reassign it on the next line. You can do it all in one line: `char[] zipherEncoded = input.ToCharArray();` – Rufus L Nov 23 '22 at 00:48
  • 1
    Required reading: [What is an IndexOutOfRangeException / ArgumentOutOfRangeException and how do I fix it?](https://stackoverflow.com/questions/20940979/what-is-an-indexoutofrangeexception-argumentoutofrangeexception-and-how-do-i-f) ...or something – Ňɏssa Pøngjǣrdenlarp Nov 23 '22 at 00:53
  • Ahh yes you are right, overlooked that lol. Thanks. – Stiffeno Nov 23 '22 at 00:53

1 Answers1

0

The problem is here:

ZipherEncode(zipherEncoded[i], codeShift[i])

In this code, i represents an index in the input string, but you're also using it as an index in the codeShift array. Since the input string may be longer than the array, you'll get an out of bounds error.

One way to resolve this would be to use a modulus operation (the% operator), which returns the remainder of a division. This can be used to always constrain the index to a value within the size of the codeShift array.

For example, let's pretend that the word has 6 characters but we only have 3 items in the codeShift array. We're fine for indexes 0, 1, and 2, but when we get to 3 we need to access the value at 0 again in the array. To do this, we can say:

int codeShiftIndex = i % codeShift.Length;

As we stated, codeShift has a length of 3, so when i is 3, i % 3 == 0 (the remainder of three divided by three), and when i is 4, i % 3 == 1, etc.

So your code change would look like:

ZipherEncode(zipherEncoded[i], codeShift[i % codeShift.Length])
Rufus L
  • 36,127
  • 5
  • 30
  • 43
  • Thank you very much Rufus that worked perfectly! It now is encoding perfectly! Please enter a word to encode: thisiscurrentlybeingencoded ENCODED: xjovixdytxhnymcdklnlfreugei – Stiffeno Nov 23 '22 at 01:15