Click here to Skip to main content
15,949,686 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Dear All,
I want to convert number string entered in a textbox into integer array, and later on getting alphabet according index value of character array, and value of integer array.
Although, by using lambda expression, I can easily achieve that. But I want to achieve the same without lambda expression, and alternative regular expression for the same. 
Note:- In given below code, without lambda expression, if I enter "0AB12", then it will print AABBC, instead of I want to ignore alphabet, and just want to decode in integer. While with lambda expression, it gives me perfect answer- ABC

Please help, it was asked me in a recent interview as a fresher.

protected void btn_Decode_Click(object sender, EventArgs e)
    {
        char[] arr = new char[] {'A','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' };
        string result = string.Empty;
        //string input = '0231';
        var myString = txt_Number.Text;
        int myInt;
        int[] num_arr = new int[myString.Length];
        for (int i = 0; i <myString.Length; i++)
        {
             if (int.TryParse(myString.Substring(i,1),out myInt))
            {
              num_arr[i] = int.Parse(myString.Substring(i, 1));
            }
            

        }
        //int myInt;


        //var num_arr = myString.ToCharArray().Where(x => int.TryParse(x.ToString(), out myInt)).Select(x => int.Parse(x.ToString())).ToArray();
        //int[] num_arr = new int[] { 0, 2, 3, 1 };


        for (int i = 0; i < num_arr.Length; i++)
        {
            result = result + arr[num_arr[i]];
        }

        Response.Write(result); 

    }


What I have tried:

protected void btn_Decode_Click(object sender, EventArgs e)
    {
        char[] arr = new char[] {'A','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' };
        string result = string.Empty;
        //string input = '0231';
        var myString = txt_Number.Text;
        int myInt;
        int[] num_arr = new int[myString.Length];
        for (int i = 0; i <myString.Length; i++)
        {
             if (int.TryParse(myString.Substring(i,1),out myInt))
            {
              num_arr[i] = int.Parse(myString.Substring(i, 1));
            }
            

        }
        //int myInt;


        //var num_arr = myString.ToCharArray().Where(x => int.TryParse(x.ToString(), out myInt)).Select(x => int.Parse(x.ToString())).ToArray();
        //int[] num_arr = new int[] { 0, 2, 3, 1 };


        for (int i = 0; i < num_arr.Length; i++)
        {
            result = result + arr[num_arr[i]];
        }

        Response.Write(result); 

    }
Posted
Updated 30-Jan-17 21:26pm

1 solution

Basically, what you are doing isn't going to work: even your lambda version will fail.
Try this: enter a "number string" that will generate the whole alphabet, and process it to generate the string.
What you get will not be the whole alphabet: all you will get is "ABCDEFGHIJBABBBCBD..." because your code expects each number to be "encoded" in a single character, and a single character cannot contain a number which translates to "10", or any value above that. So when you feed it "0123456789101112..." the "1" of "10" is interpreted as a "B", and the "0" as an "A" instead of interpreting the pair as a "K" - and unless you either change to fixed size input so each value always takes two characters, expand the range of values to Base 26 in which case int.Parse won't work, or add a separator between each "number" you cannot get this to work.
 
Share this answer
 
Comments
Ravindra_IND 31-Jan-17 3:42am    
Dear,
Lambda expression gives 100% result in this, you can try my code. It works.
OriginalGriff 31-Jan-17 3:54am    
I did. It doesn't... Try it with "0123456789101112" and see if it gives you "ABCDEFGHIJKLM" ...
Ravindra_IND 31-Jan-17 4:02am    
Well, I can't test it now, but sure will check it later. But Can you please provide working code for that without?????
OriginalGriff 31-Jan-17 4:09am    
No, because your whole idea is wrong. You need to rethink what you are doing before you rush into changing code that will have to be re-written when you fix the fundamental problems.
Ravindra_IND 31-Jan-17 4:39am    
OK..I got your point. Thanks...but please please please share me any working code for this........

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900