【问题标题】:Check whether a string is palindrome or not without using string functions and without using loop statements in C#在不使用字符串函数和不使用 C# 中的循环语句的情况下检查字符串是否为回文
【发布时间】:2016-07-13 10:40:02
【问题描述】:

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

【问题讨论】:

  • 不清楚“不使用字符串函数”是什么意思,但如果允许使用.Length[],则可以通过递归来实现。
  • 您是否尝试过任何给出的答案?

标签: c# arrays console-application string-function


【解决方案1】:

不管你是否必须循环字符串;但您可以隐藏循环,使其隐式,例如

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

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

【讨论】:

  • 您不能只比较s[i] == s[s.Length - 1 -i],因为名称也可以是一个以大写字母开头并以小字母结尾的回文。例如Anna 是回文,但你的结果是假的
  • @fubo: 如果我们想检测不区分大小写回文,我们必须输入.All(i => char.ToUpper(s[i]) == char.ToUpper(s[s.Length - 1 -i]))并准备好争论:“我们不是允许使用 STRING 方法,而不是 CHAR 方法" ;)
  • @fubo 我认为为了回答问题我们应该忽略大小写;恕我直言,这是对问题核心的干扰。
【解决方案2】:

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

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 和字符串数组运算符不被视为“字符串函数”。

【讨论】:

    【解决方案3】:

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

    (安娜 = 真,安娜 = 假)

    代码:

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

    【讨论】:

    【解决方案4】:

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

    但是,所有 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() 的论点。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-07-29
      • 2021-04-18
      • 2015-07-18
      • 2019-12-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多