Showing posts with label Code. Show all posts
Showing posts with label Code. Show all posts

Monday, January 14, 2008

Function to Reverse a String in C#

 public static String Reverse(String strParam)
{
if(strParam.Length==1)
{
return strParam;
}
else
{
return Reverse(strParam.Substring(1)) + strParam.Substring(0,1);
}
}

Tuesday, March 27, 2007

Funtion to Find whether the String is Palindrome or Not

using System;

class test
{

private static void Main()
{

Console.WriteLine("Is 'ada' Palindrome : {0}",IsPalindrome("ada"));
Console.ReadLine();
}

public static bool IsPalindrome(String strParam)
{
int iLength,iHalfLength;
iLength = strParam.Length - 1;
iHalfLength = iLength/2;

for(int iIndex=0;iIndex<=iHalfLength;iIndex++)
{
if(strParam.Substring(iIndex,1)!=strParam.Substring(iLength - iIndex,1))
{
return false;
}
}
return true;
}
}



Monday, March 26, 2007

Celsius to Fahrenheit

int Celsius2Fahrenheit(int Celsius)
{
return 9*Celsius/5+32;
}

limitation: what if we want to pass a floating point number?

String Date Validator in C#

Simple string date validator.

I am a big fan of maintaining a library of simple and clean helper methods. Here is a simple and clean way to verify if a string formatted date is a valid date. This allows you to encapsulate the exception handling making it easy to use and very readable - another important coding practice.

private static bool IsDate(string sDate)
{
DateTime dt;
bool isDate = true;

try
{
dt = DateTime.Parse(sDate);
}
catch
{
isDate = false;
}

return isDate;
}