【发布时间】: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