我用c#做了一个回文程序。为了消除区分大小写,我必须将输入完全转换为小写(或大写)。现在,当我打印反向数组时,我希望恢复区分大小写。比如if input = Madam,我希望Reverse = madaM。现在我知道我可以从输入中创建一个新的 char 数组,而且我已经做到了。但我想以某种方式操纵 LowerInvariant 数组。我知道我的代码的 Upper 方法不起作用,因为数组已转换为小写。
using System;
using System.Linq;
public class Palindrome
{
public static void Main(string [] args)
{
string input = Console.ReadLine();
string lower = input.ToLowerInvariant();
char[] array = lower.ToCharArray();
Array.Reverse(array);
Upper(array, input);
Console.WriteLine("\nPalindrome: {0}", array.SequenceEqual(lower));
Console.ReadKey();
}
public static void Upper(char[] array, string input)
{
for (int i = 0; i < array.Length; i++)
{
if (Input's [i] char is Upper) => Not sure how to put this as a code.
array[i] = Char.ToUpper(array[i]);
}
Console.WriteLine("Reverse array :");
for (int i = 0; i < array.Length; i++)
Console.Write("{0}", array[i]);
}
}
最佳答案
这应该做你想做的:
public static void Upper(char[] array, string input)
{
Console.WriteLine("Reverse array :");
Console.Write(new string(input.AsEnumerable().Reverse().ToArray()));
}
不必担心小写数组,您可以简单地反转原始输入并从反转的字符中创建一个新字符串。
关于c# - 将完全小写/大写字符数组转换回用户输入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27155547/