使用C#将整数字符串解码为字母表 [英] Decode integer string into alphabet using C#

查看:86
本文介绍了使用C#将整数字符串解码为字母表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

亲爱的所有,
我想将文本框中输入的数字字符串转换为整数数组,然后根据字符数组的索引值和整数数组的值获取字母。
虽然通过使用lambda表达式,我可以轻松实现这一点。但是我希望在没有lambda表达式的情况下实现相同的目标,并为其提供替代正则表达式。
注意: - 在下面给出的代码中,没有lambda表达式,如果我输入0AB12,那么它将打印AABBC,而不是我想忽略字母表,只想整数解码。使用lambda表达式时,它给了我完美的答案 - ABC

请帮助,它在最近的采访中被问到我是一个新鲜的。

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()。其中​​(x => int.TryParse(x.ToString(),out myInt))。选择(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);

}





我的尝试:



 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()。其中​​(x => int.TryParse(x.ToString(),out myInt))。选择(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);

}

解决方案

基本上,你正在做的事情是行不通的:甚至你的lambda版本将失败。

试试这个:输入一个数字字符串,它将生成整个字母表,并处理它以生成字符串。

你得到的不会是整个字母表:你得到的只是ABCDEFGHIJBABBBCBD ...因为你的代码希望每个数字在一个字符中被编码,而一个字符不能包含一个转换为10的数字,或者高于该数字的任何值。所以当你喂它0123456789101112 ......时,10的1被解释为B,而0被解释为A而不是将这对解释为K - 除非你要么改为固定大小输入所以每个值总是需要两个字符,将值的范围扩展到Base 26,在这种情况下int.Parse将不起作用,或者在每个数字之间添加一个分隔符,你无法使其工作。

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); 

    }

解决方案

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.


这篇关于使用C#将整数字符串解码为字母表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆