How to return to the beginning of a C#Array

Asked

Viewed 242 times

1

I have to develop a code where he does the Cesar Cipher encryption, the characters I should use should be in this sequence; A~Z,(blank space),0~9. I’ve developed most of the code, only I ran into 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 on the following line ASCIIC = tabelaASCII[y + 4]; How do I go back to the beginning of the array when the sum of the index overflows?

Example:

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

resultado = array[y+4];
/* resultado = 1 */
  • The answer below helped you, please vote or from your comment on helping, to improve the question.

1 answer

0


This should solve your problem, when you pop the index it goes back 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;
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.