是否有可能写一个ROT13在一条线? [英] Is it possible to write a ROT13 in one line?
问题描述
我有下面的代码,我希望看到一个oneliner。然而,因为我很新的C#,我目前没有如何做到这一点...
I have the following code which I would like to see as a oneliner. However, since I am very new to C#, I currently have no clue on how to do this...
代码没有线索:
static string ROT13 (string input)
{
if (string.IsNullOrEmpty(input)) return input;
char[] buffer = new char[input.Length];
for (int i = 0; i < input.Length; i++)
{
char c = input[i];
if (c >= 97 && c <= 122)
{
int j = c + 13;
if (j > 122) j -= 26;
buffer[i] = (char)j;
}
else if (c >= 65 && c <= 90)
{
int j = c + 13;
if (j > 90) j -= 26;
buffer[i] = (char)j;
}
else
{
buffer[i] = (char)c;
}
}
return new string(buffer);
}
我很抱歉的任何不便,只是想更多地了解这个美丽的语言:)
I am sorry for any inconvenience, just trying to learn more about this pretty language :)
推荐答案
这个怎么样?
我只是碰巧有这样的代码躺在附近,它不漂亮,但它的工作。
只是为了确保:单行是乐趣,但他们通常不会提高可读性和代码的可维护性。所以我会坚持自己的解决方案:)
What about this? I just happen to have this code lying around, it isn't pretty, but it does the job. Just to make sure: One liners are fun, but they usually do not improve readability and code maintainability... So I'd stick to your own solution :)
static string ROT13(string input)
{
return !string.IsNullOrEmpty(input) ? new string (input.ToCharArray().Select(s => { return (char)(( s >= 97 && s <= 122 ) ? ( (s + 13 > 122 ) ? s - 13 : s + 13) : ( s >= 65 && s <= 90 ? (s + 13 > 90 ? s - 13 : s + 13) : s )); }).ToArray() ) : input;
}
如果你需要进一步澄清,只是要求。
If you need more clarification, just ask.
这篇关于是否有可能写一个ROT13在一条线?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!