c# - 不使用字符串函数也不使用C#中的循环语句来检查字符串是否是回文

标签 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();
        }
    }
}

最佳答案

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/

相关文章:

c# - 如何在 C# 中将 Dim 代码编写为 New System.Windows.Forms.Binding ("text", Me.bindingSource1, "ATMCode", True)

android - 如何获取 JSONArray 的第一个值?

javascript - 为什么这在 JS 而不是 PHP 中有效?

c# - 在控制台应用程序下找不到 System.drawing 命名空间

c# - 推荐的 .NET 加密库

c# - 应该使用委托(delegate)的一些常见场景是什么?

c# - 为什么我的 StackPanel 不在生成的类中? (空引用异常)

php - 通过PHP/MySQL构建多维JSON数组

java - 如何重新分配已输入的基于扫描仪的值? - java

c++ - 在 Visual Studio 2012 控制台应用程序中创建一个窗口?