val*_*abu 1 c# arrays console-application string-function
在不使用字符串函数且不在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();
}
}
}
Run Code Online (Sandbox Code Playgroud)
不管怎样,你必须在字符串上循环 ; 但你可以隐藏循环,使其隐含,例如
bool result = Enumerable
.Range(0, s.Length)
.All(i => s[i] == s[s.Length - 1 - i]);
Run Code Online (Sandbox Code Playgroud)
当然,这种解决方案也很接近作弊
| 归档时间: |
|
| 查看次数: |
4787 次 |
| 最近记录: |