字符串和数字的回文计算 [英] Palindrome Calculation for both a string and a number

查看:107
本文介绍了字符串和数字的回文计算的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

海,
我想在ASP.NET中查找数字或字符串是否是回文.

谢谢与问候,
Sukanya Narayanan.

Hai,
I want to find whether a number or a string is a palindrome or not in ASP.NET.

Thanks & Regards,
Sukanya Narayanan.

推荐答案

char [] strArray = txtCustName.Text.ToCharArray();
Array.Reverse(strArray);
字符串strReversed =新字符串(strArray);
字符串reverseString = strReversed;
如果(reverseString == txtCustName.Text)
{
Label1.Text ="It is palimdromae";
}
char[] strArray = txtCustName.Text.ToCharArray();
Array.Reverse(strArray);
string strReversed = new string(strArray);
string reverseString = strReversed;
if (reverseString == txtCustName.Text)
{
Label1.Text ="It is palimdromae";
}


使用以下功能

私人静态布尔值Pal(string s){
for(int i = 0; i< s.Length; i ++){
if(s [i]!= s [s.Length-1-i]){
返回false;
}
}
返回true;
}


公共静态布尔IsPalindrome(string palindromeCandidate)
{
如果(string.IsNullOrEmpty(palindromeCandidate))
{
返回true;
}
正则表达式nonAlphaChars =新正则表达式("[^ a-z0-9]");
字符串alphaOnlyCandidate = nonAlphaChars.Replace(palindromeCandidate.ToLower(),");
如果(string.IsNullOrEmpty(alphaOnlyCandidate))
{
返回true;
}
int leftIndex = 0;
int rightIndex = alphaOnlyCandidate.Length-1;
一会儿(rightIndex> leftIndex)
{
如果(alphaOnlyCandidate [leftIndex]!= alphaOnlyCandidate [rightIndex])
{
返回false;
}
leftIndex ++;
rightIndex--;
}
返回true;
}
use the below function

private static bool Pal(string s) {
for (int i = 0; i < s.Length; i++) {
if (s[i] != s[s.Length - 1 - i]) {
return false;
}
}
return true;
}

OR
public static bool IsPalindrome(string palindromeCandidate)
{
if (string.IsNullOrEmpty(palindromeCandidate))
{
return true;
}
Regex nonAlphaChars = new Regex("[^a-z0-9]");
string alphaOnlyCandidate = nonAlphaChars.Replace(palindromeCandidate.ToLower(), "");
if (string.IsNullOrEmpty(alphaOnlyCandidate))
{
return true;
}
int leftIndex = 0;
int rightIndex = alphaOnlyCandidate.Length - 1;
while (rightIndex > leftIndex)
{
if (alphaOnlyCandidate[leftIndex] != alphaOnlyCandidate[rightIndex])
{
return false;
}
leftIndex++;
rightIndex--;
}
return true;
}


找到以下链接,这将对您有帮助

http://www.eggheadcafe.com/community/aspnet/2/10048946/how-to-find-that-given-string-is-palindrome-or-not.aspx
Find the below link this will help you

http://www.eggheadcafe.com/community/aspnet/2/10048946/how-to-find-that-given-string-is-palindrome-or-not.aspx


这篇关于字符串和数字的回文计算的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆