Change characters within a string

1

I can invert two numbers of an array with the array.reverse but, in this case, I have declared a String that you enter by the input method and that this is transformed into an array with the Split method separated by ',' .

In the chain these combinations can exist: 1,12,2,23,3,34,4,14 .

What I need is that if you enter by keyboard 21 you invert it to 12 , as with 32 , that is reversed to 23 etc.

My question is the following to see if I explain myself well. I have stored in the array numbers between 1 and 4 combined example: 12,32,41,24). Leaving aside that there are combinations that can not exist due to the requirements of my exercise, I need to look inside the positions of the array if the number is ordered from least to greatest and if it is not changing it. Currently my program does not solve this problem just put me left right up down etc when I insert a number but it has to be well ordered (from lowest to highest), if I do not order it shows me the number without more, not the enum to which refers. I think I would have to first look at the index [i] of the array and buy the number that is inside it but being a two-digit number you will not buy the first character with the second one.

  using System;
  using System.Collections.Generic;
  using System.Linq;
  using System.Text;
  using System.Threading.Tasks;

  namespace Ejercicio3
  {
      class Program
      {
          enum direcion { arriba = 1, abajo = 3, derecha = 2, izquieda = 4, 
  abajoDerecha=23, abajoIzquierda=34,arribaDerecha=12,arribaIzquieda=14, 
  prohibido };
          static void Main(string[] args)
          {
              string num = "";
              Console.WriteLine("Escribe la direcion que desees tomar");
              num = Console.ReadLine();
              string[] array = num.Split(',');

              for (int i = 0; i < array.Length; i++)
              {
                  if (array[i].Length==2)
                 {
                  var a=int.Parse(array[i].Substring(0, 1));
                var b=int.Parse(array[i].Substring(1,2));
                {
                    if (a < b)
                    {
                        array[i].Reverse;
                    }

                }
                  }
                  Console.WriteLine("{0}", (direcion)int.Parse(array[i]));
              }     

          }
      }
  }
    
asked by winnie 16.10.2018 в 13:08
source

1 answer

1

The array.reverse method, according to its definition what it does is invert the order of certain elements of an array, among them.

What you want is not that.

Your array has numbers in each position, and what you want is to transform numbers as if they were positional, in ordered numbers.

So if you have the number 41, what you want is for 14 to appear ...

So, you have everything turned out, as you did, making only a small change ...

Let's see, if a is < b , then the number would be fine ... if it's the other way around, we should change the number ..

I would not work them as numbers, since as a string, it's just as easy, and then it will be easier to reconfigure them.

string a = array[i].Substring(0, 1);
string b = array[i].Substring(1, 1);
if (a.CompareTo(b) > 0)
{
    array[i] = b + a;
}

Try with that, sure you will simplify things.

    
answered by 16.10.2018 / 16:05
source