1

在不使用字符串函数和不使用 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();
        }
    }
}
4

4 回答 4

5

无论如何,您必须遍历字符串;但你可以隐藏循环,使其隐含,例如

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

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

于 2016-07-13T10:45:09.000 回答
4

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

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注意:出于问题的目的,我不得不假设字符串数组运算符不被视为“字符串函数”。

于 2016-07-13T11:03:30.743 回答
2

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

(安娜 = 真,安娜 = 假)

代码:

string s = Console.ReadLine();
bool isPalindrome = s.SequenceEqual(s.Reverse());
于 2016-07-13T10:41:30.883 回答
0

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

但是,所有 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);

请记住关于 char.ToUpper() 的论点。

于 2016-07-16T08:11:23.343 回答