检查字符串是否为回文,而不使用字符串函数,也不使用 C# 中的循环语句

本文关键字:字符串 语句 循环 函数 是否 检查 回文 | 更新日期: 2023-09-27 17:56:56

检查字符串是否为回文,而不使用字符串函数,也不使用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();
        }
    }
}

检查字符串是否为回文,而不使用字符串函数,也不使用 C# 中的循环语句

不管怎样,你必须遍历字符串;但你可以隐藏循环,让它隐,例如

  bool result = Enumerable
    .Range(0, s.Length)
    .All(i => s[i] == s[s.Length - 1 - i]);

当然这个解决方案和类似的东西都接近作弊

你可以使用递归吗?因为如果是这样:

class Program
{
    static void Main()
    {
        Console.WriteLine(IsPalindrome("ABCDEFG")); // Prints false
        Console.WriteLine(IsPalindrome("ABCDCBA")); // Prints true
    }
    public static bool IsPalindrome(string text)
    {
        return isPalindrome(0, text.Length - 1, text);
    }
    private static bool isPalindrome(int indexOfFirst, int indexOfLast, string text)
    {
        if (indexOfFirst >= indexOfLast)
            return true;
        if (text[indexOfFirst] != text[indexOfLast])
            return false;
        return isPalindrome(indexOfFirst + 1, indexOfLast - 1, text);
    }
}

那里没有循环 - 甚至没有任何隐藏在被调用的方法中的偷偷摸摸的小循环。

注意:就问题而言,我不得不假设string.Length和字符串数组运算符不被视为"字符串函数"。

更新了我的代码。没有循环,也没有字符串方法。忽略区分大小写。

(安娜 = 真,安娜 = 假)

法典:

string s = Console.ReadLine();
bool isPalindrome = s.SequenceEqual(s.Reverse());

可以肯定地说,每个人都在技术上是正确的,因为我们都避免直接使用循环和实际的字符串函数?

但是,所有 Enumerable 扩展方法肯定会在某个时候使用循环。 在遍历数组时无法避免使用循环。 除非您提前知道数组中有多少元素,并且在代码中显式处理该数组中的每个元素(尽管这将是很多代码)。

我将答案的变体(递归除外)放在一起,并用计时器进行 1,000,000 次迭代。 当您运行它们时,您会发现时间非常有趣;我使用了控制台应用程序。

        var lWatch = new Stopwatch();
        var s = "ABCDCBA";
        bool result;
        bool isPalindrome;
        ////Simple array element comparison
        lWatch.Restart();
        for (int j = 0; j < 1000000; j++)
            result = Enumerable
                .Range(0, s.Length)
                .All(i => s[i] == s[s.Length - 1 - i]);
        lWatch.Stop();
        Console.WriteLine(lWatch.Elapsed);

        ////Sequence reversal and comparison
        lWatch.Restart();
        for (int j = 0; j < 1000000; j++)
            isPalindrome = s.SequenceEqual(s.Reverse());
        lWatch.Stop();
        Console.WriteLine(lWatch.Elapsed);

        ////Simple array element comparison; respecting casing
        lWatch.Restart();
        for (int j = 0; j < 1000000; j++)
            result = Enumerable
                .Range(0, s.Length)
                .All(i => char.ToUpper(s[i]) == char.ToUpper(s[s.Length - 1 - i]));
        lWatch.Stop();
        Console.WriteLine(lWatch.Elapsed);

        ////Sequence reversal and comparison; respecting casing
        lWatch.Restart();
        for (int j = 0; j < 1000000; j++)
            isPalindrome = s.Select(c => char.ToUpper(c)).SequenceEqual(s.Select(c => char.ToUpper(c)).Reverse());
        lWatch.Stop();
        Console.WriteLine(lWatch.Elapsed);

请记住关于字符的争论。ToUpper()。