How to return to the beginning of an Array in C #

1

I have to develop a code where it does the Cipher of Cesar encryption, the characters that I should use should be in this sequence; A ~ Z, (whitespace), 0 ~ 9. I already developed a lot of the code, but I came across a problem;

for (int i = 0; i < palavra.Length; i++){
//Pego Valor Dec. da palavra
ASCIIP = (int)palavra[i], x = 1, y = 0;
    while (x != 0){
          //Verifico qual índice esta o valor dec.
          if (ASCIIP == tabelaASCII[y]){
             //Somo a chave da cifra
             ASCIIC = tabelaASCII[y + 4];
             //Converto para Char o valor Criptografado
             encrypt += Char.ConvertFromUtf32(ASCIIC);
             x = 0;
             }
          y++;
    }
   return encrypt;
}

My problem is in the following line ASCIIC = tabelaASCII[y + 4]; How do I go back to the beginning of the array when the sum of the index pops up?

Example:

int [] array = {1,2,3,4,5}, y = 1, resultado;
resultado = array[y];
/* resultado = 2 */

resultado = array[y+4];
/* resultado = 1 */
    
asked by anonymous 23.11.2016 / 17:14

1 answer

0

This should solve your problem, when it bursts the index it returns to zero.

for (int i = 0; i < palavra.Length; i++){
//Pego Valor Dec. da palavra
ASCIIP = (int)palavra[i], x = 1, y = 0, num = 4;
    while (x != 0){
          //Verifico qual índice esta o valor dec.
          if (ASCIIP == tabelaASCII[y]){
             num = num == palavra.Length ? 0 : num;
             //Somo a chave da cifra
             ASCIIC = tabelaASCII[num];
             //Converto para Char o valor Criptografado
             encrypt += Char.ConvertFromUtf32(ASCIIC);
             x = 0;
             }
          num++;   
          y++;
    }
   return encrypt;
}
    
23.11.2016 / 17:23