How to add the characters of a string type sentence in a matrix?

0

I need to divide the characters of a sentence and eliminate their spaces and place them in a matrix, the order of the matrix will depend on the number of characters in the sentence, eh made this code but the error I have is that it shows me the last letter of the sentence in all positions of the matrix:

        char[,] A;
        int orden;
        int x;
        string cadena = txtfrase.Text.Trim();
        orden = int.Parse(cadena.Length.ToString());
        if (orden % 2 == 0)
        {
            x = orden;
            if (orden >= 0)
            {
                int i;
                for (i = 0; i < 20; i++)
                    x = (((x * x) + orden) / (2 * x));
            }
            A = new char[x, x];
            for (int i = 0; i < A.GetLength(0); i++)
            {
                for (int j = 0; j < A.GetLength(1); j++)
                {
                    for (int l = 0; l < cadena.Length; l++)
                        A[i, j] = cadena[l]; 

                }
            }
    
asked by Duval Carvajal 27.06.2018 в 00:55
source

1 answer

0

in this line you are indicating just that

for (int l = 0; l < cadena.Length; l++)
   A[i, j] = cadena[l]; 

at the end of the A[i, j] will contain the last character

You would have to use an l external to the loops of i and j and go increasing it as you assign the value

wave

l=0;
for (int i = 0; i < A.GetLength(0); i++) {
  for (int j = 0; j < A.GetLength(1); j++) {
    A[i, j] = cadena[l%cadena.Length]; 
    l++;
  }
}
    
answered by 27.06.2018 в 04:08