Thursday, May 20, 2010

String Palindrome, Number Palindrome using c#.net

//string palindrome
string revstr = "";
public string palindrome(string s)
{

for (int i = s.Length - 1; i >= 0; i--)
{

revstr += s[i];

}
return revstr;

////string palindrome using c# predefined methods
//char[] chstr = s.ToCharArray();
//Array.Reverse(chstr);
//string str = new string(chstr);
//return str;
}
////number palindrome
public int NumPalindrome(int n)
{
int r;
int num=0;
while (n > 0)
{
r = n % 10;
n = n / 10;
num = num * 10 + r;
}
return num;
}

No comments:

Post a Comment