不使用字符串函数也不使用C#中的循环语句来检查字符串是否是回文。我可以不用字符串函数,但是我不知道如何不用循环语句检查。我在一次采访中面临这个问题。
using System;
namespace palindrome
{
class Program
{
static void Main(string[] args)
{
string s,revs="";
Console.WriteLine(" Enter string");
s = Console.ReadLine();
for (int i = s.Length-1; i >=0; i--) //**i want to replace this for loop**
{
revs += s[i].ToString();
}
if (revs == s) // Checking whether string is palindrome or not
{
Console.WriteLine("String is Palindrome \n Entered String Was {0} and reverse string is {1}", s, revs);
}
else
{
Console.WriteLine("String is not Palindrome \n Entered String Was {0} and reverse string is {1}", s, revs);
}
Console.ReadKey();
}
}
}
最佳答案
Willy-nilly,您必须遍历字符串;但您可以隐藏循环,使其隐式显示,例如
bool result = Enumerable
.Range(0, s.Length)
.All(i => s[i] == s[s.Length - 1 - i]);
确保这个解决方案和类似的东西都接近作弊
关于c# - 不使用字符串函数也不使用C#中的循环语句来检查字符串是否是回文,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38349346/