C# 在不使用字符串函数和C中的循环语句的情况下,检查字符串是否为回文
如果不使用字符串函数和C中的循环语句,检查字符串是否是回文的。我可以不使用字符串函数,但不知道如何在不使用循环语句的情况下进行检查。我在一次采访中遇到了这个问题C# 在不使用字符串函数和C中的循环语句的情况下,检查字符串是否为回文,c#,arrays,console-application,string-function,C#,Arrays,Console Application,String Function,如果不使用字符串函数和C中的循环语句,检查字符串是否是回文的。我可以不使用字符串函数,但不知道如何在不使用循环语句的情况下进行检查。我在一次采访中遇到了这个问题 using System; namespace palindrome { class Program { static void Main(string[] args) { string s,revs=""; Console.WriteLin
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();
}
}
}
更新了我的代码。没有循环和字符串方法。区分大小写被忽略了 安娜=对,安娜=错 代码:
更新了我的代码。没有循环和字符串方法。区分大小写被忽略了 安娜=对,安娜=错 代码: 不管你愿意与否,你都必须在绳子上打圈;但是可以隐藏循环,使其隐式,例如 确保此解决方案和类似方案接近作弊你必须在字符串上循环;但是可以隐藏循环,使其隐式,例如
确定这个解决方案和类似的解决方案接近作弊是否允许使用递归?因为如果是这样:
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数组运算符在本问题中不被视为字符串函数。是否允许使用递归?因为如果是这样:
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数组运算符在本问题中不被视为字符串函数。可以安全地说每个人在技术上都是正确的,因为我们都避免直接使用循环和实际的字符串函数吗 但是,所有可枚举扩展方法在某个点上肯定会使用循环。在遍历数组时,无法避免使用循环。除非您提前知道数组中有多少个元素,并且在代码中显式地处理该数组中的每个元素,否则代码将非常多 我将答案的变体(递归除外)放在一起,并用计时器装饰1000000次迭代。当你运行它们时,你会发现计时非常有趣;我使用了一个控制台应用程序
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的争论。可以安全地说每个人在技术上都是正确的,因为我们都避免直接使用循环和实际的字符串函数吗 但是,所有可枚举扩展方法在某个点上肯定会使用循环。在遍历数组时,无法避免使用循环。除非您提前知道数组中有多少个元素,并且在代码中显式地处理该数组中的每个元素,否则代码将非常多 我将答案的变体(递归除外)放在一起,并用计时器装饰1000000次迭代。当你运行它们时,你会发现计时非常有趣;我使用了一个控制台应用程序
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的争论。不清楚不使用字符串函数意味着什么,但是如果允许使用.Length和[],可以使用递归来实现。您尝试过任何给定的答案吗?不清楚不使用字符串函数意味着什么,但是如果允许使用.Length和[],您可以使用递归来实现这一点。您是否尝试过任何给定的答案?我想要不带字符串的内置函数。@Valluriratnlabu否,反向来自System.Linq.Enumerable,并且不是我想要不带字符串内置函数的字符串内置函数。@Valluriratnlabu否,Reverse来自System.Linq.Enumerable,不是一个内置的字符串函数。您不能只比较s[i]==s[s.Length-1-i],因为名称也可以是以大写字母开头,以小写字母结尾的回文。e、 安娜是一个回文,但你的结果将是false@fubo:如果我们想检测不区分大小写的回文,我们必须放入.Alli=>char.ToUppers[i]==char.ToUppers[s.Length-1-i],并准备好争论:我们不允许使用字符串方法,而不是char方法@我认为,为了回答这个问题,我们应该忽略这个案例;你不能只比较s[i]==s[s.Length-1-i],因为名字也可以是以大写字母开头,以小写字母结尾的回文。e、 安娜是一个回文,但你的结果将是false@fubo:如果我们想检测不区分大小写的回文,我们必须放入.Alli=>char.ToUppers[i]==char.ToUppers[s.Length-1-i],并准备好争论:我们不允许使用字符串方法,而不是char方法@我认为,为了回答这个问题,我们应该忽略这个案例;这是从问题的核心转移注意力。