在C#中编写一个函数,检查给定的句子是否是回文。 [英] Write a function in C# that checks if a given sentence is a palindrome.
问题描述
在C#中编写一个函数,检查给定的句子是否是回文。回文是一个单词,短语,诗句或句子,向后或向前读取相同的词。只考虑英文字母(AZ和az)的顺序,其他字符应该被忽略。
例如,IsPalindrome(Noel看到Leon。)应该返回true,因为空格,句点和大小写应该被忽略,因为它是向后和向前读取的noelseesleon,这是一个回文。
什么我试过了:
Write a function in C# that checks if a given sentence is a palindrome. A palindrome is a word, phrase, verse, or sentence that reads the same backward or forward. Only the order of English alphabet letters (A-Z and a-z) should be considered, other characters should be ignored.
For example, IsPalindrome("Noel sees Leon.") should return true as spaces, period, and case should be ignored resulting with "noelseesleon" which is a palindrome since it reads same backward and forward.
What I have tried:
using System;
public class Palindrome
{
public static bool IsPalindrome(string str)
{
string rev = "";
//string [] words
for(int i = input1.Length-1; i >= 0; i--)
{
rev += input1[i];
}
if(str.ToLower() == rev.ToLower())
{
return true;
}else{
return false;
}
}
public static void Main(string[] args)
{
Console.WriteLine(IsPalindrome("Noel sees Leon."));
}
}
推荐答案
您可以在这里找到大量答案/解决方案/提示/文章:< a href =http://www.codeproject.com/search.aspx?q=palindrome+tag%3ac%23>搜索 - CodeProject [ ^ ]
Tons of answers/solutions/tips/articles you'll find here: Search - CodeProject[^]
试试这个
Try this
class Program
{
static void Main(string[] args)
{
Console.WriteLine(IsPalindrome("Noel sees Leon."));
Console.ReadLine();
}
public static bool IsPalindrome(string str)
{
string forward = "";
foreach (char c in str.ToLower()) // ToLower to ignore the case
if (char.IsLetter(c)) // ignore the period, space and other char except alphabets
forward += c;
char[] temp = forward.ToCharArray();
Array.Reverse(temp);
string reverese = new string(temp);
return forward == reverese;
}
}
这篇关于在C#中编写一个函数,检查给定的句子是否是回文。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!