Question

I want to convert the string to character array. Further, the values in character array has to be converted to number values using enum. The values that returned as integer has to be stored in integer array.

My coding is:

enum alphabets {  a = 1,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z}
static void Main()
    {
       int row,column;
       String value1;
       Console.WriteLine("Enter string");
       string UserString= Console.ReadLine();
       int len = UserString.Length;

       if (len % 2 == 0)
          {
             row = len / 2;
             column = len / 2;
             PrintMatrix(row, column, len, value1);
          }

    Console.ReadKey();
    }

static void PrintMatrix(int row, int column, int len, string value1)
{
    Console.WriteLine("Matrix");
    string mat_row = value1.Substring(0, row);
    string mat_col = value1.Substring(row, len - row);
    GenNum(mat_row,len);
    GenNum(mat_col,len);
}

static void GenNum(string val, int len)
{
    string res = val; 

    foreach (char c in res)
      {
         string name = c.ToString();
         alphabets parsed = (alphabets)Enum.Parse(typeof(alphabets), name);
         int NumGen = (int)parsed;
         Console.WriteLine (NumGen);
      }
}

I tried to convert the string name as char[] it is saying error like the following:

error1

so, i tried to change NumGen as int[], it says error like this:

error2

How can i solve it?

Was it helpful?

Solution

See if below is what you need, GenNum now return int[] of your string representation!

    enum alphabets { a = 1, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p, q, r, s, t, u, v, w, x, y, z }
    static void Main()
    {
        Console.WriteLine("Enter string");
        string UserString = Console.ReadLine();
        int len = UserString.Length;
        int row = 1, column = 3;

        if (len % 2 == 0)
        {
            row = len / 2;
            column = len / 2;
            PrintMatrix(row, column, len, UserString);
        }

        Console.ReadKey();
    }

    static void PrintMatrix(int row, int column, int len, string value1)
    {
        Console.WriteLine("Matrix");
        string mat_row = value1.Substring(0, row);
        string mat_col = value1.Substring(row, len - row);
        int[] arrayRow = GenNum(mat_row, len);
        int[] arrayCol = GenNum(mat_col, len);
    }

    static int[] GenNum(string val, int len)
    {
        string res = val;
        int[] intArray = new int[len];
        int index = 0;

        foreach (char c in res)
        {
            string name = c.ToString();
            alphabets parsed = (alphabets)Enum.Parse(typeof(alphabets), name);
            int NumGen = (int)parsed;
            intArray[index++] = NumGen;
            Console.WriteLine(NumGen);
        }

        return intArray;
    }

I hope this helps

OTHER TIPS

You are using this enum method to do something that can be done by simply converting the character's numeric value to an int and then subtracting from the smallest character ('a') and adding one. Of course, this only works for the Latin lowercase alphabet, but that appears to be all you are dealing with, so we can ignore the multitude of issues that can arise when doing arithmetic with numeric values of characters.

To convert a char to an int, you can use the following:

// ch is a char
int intVal = (int)ch;

You can save these in an array, you should create an array of the appropriate length (that is, the length of the input string), and then, when looping through each character of the source string, you save the int result in the right slot of the array. There are many ways of doing this. Personally, I'd use an enumerable block, like so:

private static IEnumerable<int> StringToInts(string val) {
    var aVal = (int)'a';
    foreach(var ch in val)
        yield return (int)ch - aVal + 1;
}

And then in your calling code, you can extract an array using LINQ:

var intArray = StringToInts("Some string").ToArray();
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top