public static char FirstNonRepeatedHash(string stringToCheck)
{
Hashtable cHash = new Hashtable();
int length = stringToCheck.Length;
int i = 0;
bool hashPresent = false;
char charToReturn = '\0';
for (i = 0; i < length; i++)
{
hashPresent = cHash.Contains(stringToCheck[i]);
if (hashPresent)
{
cHash[stringToCheck[i]] = 1 + Convert.ToInt32(cHash[stringToCheck[i]]);
}
else
{
cHash.Add(Convert.ToChar(stringToCheck[i]), 1);
}
}
for (i = 0; i < length; i++)
{
if (Convert.ToInt32(cHash[stringToCheck[i]]) ==1 )
{
charToReturn = stringToCheck[i];
break;
}
}
return charToReturn;
}
Monday, December 25, 2006
First Non-Repeated Character from a UNICODE String using Hash Table
the only difference here is the input string can be UNICODE (65000 characters) instead of ASCII Code (256 characters) so using a hash table makes more sense
First Non repeated Character in a String using an Array
Here is how to find the first non repeated character in an a string using an array
the assumption is the String is ASCII other wise check for the hash table function
For example If the input is "TEETOTALAR" The First non repeated Character is "O"
the assumption is the String is ASCII other wise check for the hash table function
For example If the input is "TEETOTALAR" The First non repeated Character is "O"
public static char FirstNonRepeatedArray(string stringToCheck)
{
int length = stringToCheck.Length;
int i = 0;
int[] intCollection = new int[256];
char returnChar = '\0';
for (i = 0; i < length; i++)
{
intCollection[stringToCheck[i]] = intCollection[stringToCheck[i]] + 1;
}
for (i = 0; i < length; i++)
{
if (intCollection[stringToCheck[i]] == 1)
{
returnChar = stringToCheck[i];
break;
}
}
return returnChar;
}
Saturday, November 25, 2006
String is a Palindrome !
How to check if a string is a palindrome ?
You can change the code a bit to reverse an array
You can change the code a bit to reverse an array
public static bool IsPalindrome(string s)
{
int length =s.Length;
char[] chrArray = s.ToCharArray();
if (length == 0)
return false;
if (length == 1)
return true;
int start = 0;
int end = length - 1;
while (end > start)
{
if (chrArray[start] == chrArray[end])
{
start++;
end--;
}
else
{
return false;
}
}
return true;
}
Wednesday, October 25, 2006
Remove Characters from a String with limited Memory
How about, If we don't have enough memory we have to use a single array.
Here is how to do that with limited memory
Here is how to do that with limited memory
public static string RemoveCharactersLimited(char[] s, string removeChars)
{
int i = 0, j = 0;
int lengthC = removeChars.Length;
int lengthS = s.Length;
int[] intCollection = new int[256];
for (i = 0; i < lengthC; i++)
{
intCollection[removeChars[i]] = 1;
}
i = j = 0;
for (i = 0; i < lengthS; i++)
{
if (intCollection[s[i]] != 1)
{
s[j] = s[i];
j++;
}
}
while (j < lengthS)
{
s[j] = '\0';
j++;
}
return new string(s);
}
Remove Characters from String C#.NET
The string manipulation function removes a set of characters fromthe input string
For example
Removing "RT" from "ROBERT FROST" will give you "OBE FOS"
For example
Removing "RT" from "ROBERT FROST" will give you "OBE FOS"
public static string RemoveCharacters(string s, string removeChars)
{
int i=0,j=0;
int lengthC = removeChars.Length;
int lengthS = s.Length;
int[] intCollection = new int[256];
char[] s2 = new char[lengthS];
for (i = 0; i < lengthC; i++)
{
intCollection[removeChars[i]] = 1;
}
i = j = 0;
for (i = 0; i < lengthS; i++)
{
if (intCollection[s[i]] != 1)
{
s2[j] = s[i];
j++;
}
}
return new string(s2);
}
Monday, September 25, 2006
AtoI function - String to Integer in C# .NET
If you ever want to convert a string to integer, you have to go through the following checks
public static int StringToInt(string s)
{
int length = s.Length;
int i = 0;
int lastNumber = 0;
int returnNumber = 0;
bool numberNegative = false;
int startPoint = 0;
if (s[0] == '-')
{
numberNegative = true;
startPoint = 1;
}
for (i = startPoint; i < length; i++)
{
if (s[i] == ' ')
{
continue;
}
else
{
if ((s[i] >= '0') && s[i] <= '9')
{
returnNumber = s[i] - '0';
if (i > 0) lastNumber = lastNumber * 10;
lastNumber = lastNumber + returnNumber;
}
else
{
break;
}
}
}
if (numberNegative)
lastNumber = -1 * lastNumber;
return lastNumber;
}
Sunday, August 20, 2006
Reverse Words in C Sharp.NET
Lately I started some string manipulation in C Sharp, which you will see in the series to come. Please add your comments suggestions. These are optimized for performance
Input: Robert Frost
output is Frost Robert
Here is another method to do the same using character array
Input: Robert Frost
output is Frost Robert
public static string ReverseWord(string s)
{
int length = s.Length;
int i = 0;
string[] splittedArray = s.Split(' ');
StringBuilder sb = new StringBuilder();
for (i = splittedArray.Length - 1; i >= 0; i--)
{
if (i != 0)
sb.Append(splittedArray[i] + ' ');
else
sb.Append(splittedArray[i]);
}
return sb.ToString();
}
Here is another method to do the same using character array
public static string ReverseWordChar(string s)
{
int length = s.Length;
int i = 0,j=0;
StringBuilder sb= new StringBuilder();
int startPos = length - 1;
for (i = length-1; i >=0;i--)
{
if (s[i] == ' ')
{
for (j = i+1; j <= startPos; j++)
{
sb.Append(s[j]);
}
startPos = i;
sb.Append(' ');
}
}
for (j = 0; j < startPos; j++)
{
sb.Append(s[j]);
}
return sb.ToString();
}
Tuesday, July 25, 2006
Factorial of a number using C #
Finding the factorial of a number in C#
the code is meant for finding the factorial both recursive and non recursive
the code is meant for finding the factorial both recursive and non recursive
public static int Factorial(int n)
{
int FactorialValue = 1;
for (int i = n; i > 0; i--)
{
FactorialValue = FactorialValue * i;
}
return FactorialValue;
}
public static int recursiveFactorial(int n)
{
if (n > 1)
{
return n * recursiveFactorial(n - 1);
}
else
{
return 1;
}
}
public static int recursiveFactorialIntermediate(int n, int[] nArray)
{
if (n > 1)
{
nArray[n - 1] = n * recursiveFactorialIntermediate(n - 1, nArray);
return nArray[n - 1];
}
else
{
return 1;
}
}
Sunday, June 25, 2006
largest element in the array by comparing to All - C#
To find out the largest element in the array by comparing to All elements method
This methd has an Big Oh notation performance of O(n2)
This methd has an Big Oh notation performance of O(n2)
public static int GetIndexLargestCompareToAll(int[] numbers)
{
int currentMaxIndex = -1;
int length = numbers.Length;
bool isMax = false;
try
{
if (length == 0)
currentMaxIndex = -1;
else if (length == 1)
currentMaxIndex = 0;
else
{
for (int i = 0; i < length; i++)
{
isMax = true;
for (int j = 0; j < length; j++)
{
if (numbers[j] > numbers[i])
isMax = false;
}
if (isMax)
currentMaxIndex = i;
}
}
}
catch (Exception)
{
currentMaxIndex = -1;
}
return currentMaxIndex;
}
Largest Element in the Array using C #
To find out the largest element in the array by comparing to the Max method
This methd has an Big Oh notation performance of O(n)
This methd has an Big Oh notation performance of O(n)
public static int GetIndexLargestCompareToMax(int[] numbers)
{
int currentMaxIndex = -1;
int length = numbers.Length;
try
{
if (length == 0)
currentMaxIndex = -1;
else if (length == 1)
currentMaxIndex = 0;
else
{
currentMaxIndex = 0;
for (int j = 1; j < length; j++)
{
if (numbers[j] > numbers[currentMaxIndex])
{
currentMaxIndex = j;
}
}
}
}
catch (Exception)
{
currentMaxIndex = -1;
}
return currentMaxIndex;
}
Thursday, May 25, 2006
Calendar Object in C Sharp.NET
A friend of mine asked me to create a calendar object in C Sharp. I thought it might be useful to others too
So here is a one for you
So here is a one for you
using System;
using System.Collections.Generic;
using System.Text;
namespace Algorithms
{
public class Calendar
{
private DateTime currentDate;
private int currentDay;
private int currentDayWeek;
private int currentMonth;
private int currentYear;
private bool leapYear;
public Calendar()
{
DateTime cDate = DateTime.Now;
currentDate = cDate;
currentDay = cDate.Day;
currentDayWeek = Convert.ToInt32(cDate.DayOfWeek);
currentMonth = cDate.Month;
currentYear = cDate.Year;
}
public Calendar(DateTime cDate)
{
currentDate = cDate;
currentDay = cDate.Day;
currentDayWeek = Convert.ToInt32(cDate.DayOfWeek);
currentMonth = cDate.Month;
currentYear = cDate.Year;
if (currentYear % 400==0)
leapYear = true;
else if (currentYear % 100==0)
leapYear = false;
else if (currentYear % 4==0)
leapYear = true;
else
leapYear = false;
}
public DateTime CurrentDate
{
get { return currentDate; }
set { currentDate = value;}
}
public int CurrentDay
{
get { return currentDay; }
set { currentDay = value; }
}
public int CurrentDayWeek
{
get { return currentDayWeek; }
set { currentDayWeek = value; }
}
public int CurrentMonth
{
get { return currentMonth; }
set { currentMonth = value; }
}
public int CurrentYear
{
get { return currentYear; }
set { currentYear= value; }
}
public bool LeapYear
{
get { return leapYear; }
}
public void Display()
{
Console.Write("== Calendar ==\n");
DateTime firstDay = currentDate.AddDays(1 - currentDay);
string dayDisplay ="";
if (Convert.ToInt32(firstDay.DayOfWeek) == 0)
{
dayDisplay = "Mo Tu We Th Fr Sa Su";
}
else if (Convert.ToInt32(firstDay.DayOfWeek) == 1)
{
dayDisplay = "Tu We Th Fr Sa Su Mo";
}
else if (Convert.ToInt32(firstDay.DayOfWeek) == 2)
{
dayDisplay = "We Th Fr Sa Su Mo Tu";
}
else if (Convert.ToInt32(firstDay.DayOfWeek) == 3)
{
dayDisplay = "Th Fr Sa Su Mo Tu We";
}
else if (Convert.ToInt32(firstDay.DayOfWeek) == 4)
{
dayDisplay = "Fr Sa Su Mo Tu We Th";
}
else if (Convert.ToInt32(firstDay.DayOfWeek) == 5)
{
dayDisplay = "Sa Su Mo Tu We Th Fr";
}
else if (Convert.ToInt32(firstDay.DayOfWeek) == 6)
{
dayDisplay = "Su Mo Tu We Th Fr Sa";
}
Console.Write(dayDisplay + "\n");
// Common content
Console.Write("01 02 03 04 05 06 07\n");
Console.Write("08 09 10 11 12 13 14\n");
Console.Write("15 16 17 18 19 20 21\n");
Console.Write("22 23 24 25 26 27 28\n");
//Variable Date
// Feb 2
if (currentMonth == 2)
{
if (leapYear)// Feb 2 leap year
Console.Write("29 00 00 00 00 00 00\n");
else
Console.Write("00 00 00 00 00 00 00\n");
}
if ((currentMonth == 4) || (currentMonth == 6) || (currentMonth == 9) || (currentMonth == 11))
{
// 4, 6, 9, 11
Console.Write("29 30 00 00 00 00 00\n");
}
if ((currentMonth == 1) || (currentMonth == 3) || (currentMonth == 5) || (currentMonth == 7) || (currentMonth == 8) || (currentMonth == 10) || (currentMonth == 12))
{
// 1,3,5,7,8,10,12
Console.Write("29 30 31 00 00 00 00\n");
}
}
}
}
Thursday, April 27, 2006
LEARN Your Way to MCAD Excellence
If you want to learn all about the .Net Windows, Web, and Xml Web Services and also get an MCAD certification as bonus.
The following tips have helped me and 10's of my friends.
I will recommend THESE and ONLY THESE books in the FOLLOWING ORDER
The ORDER of the exam is also VERY IMPORTANT and it should be
1. Windows
2. Web
3. XML Web services
For each exam FIRST you must study completely the MS Press book (100%) and THEN AND ONLY THEN study Amit Kalani's one.
All the best !"
Amazon Link LEARN Your Way to MCAD Excellence
The following tips have helped me and 10's of my friends.
I will recommend THESE and ONLY THESE books in the FOLLOWING ORDER
The ORDER of the exam is also VERY IMPORTANT and it should be
1. Windows
2. Web
3. XML Web services
For each exam FIRST you must study completely the MS Press book (100%) and THEN AND ONLY THEN study Amit Kalani's one.
All the best !"
Amazon Link LEARN Your Way to MCAD Excellence
Tuesday, April 25, 2006
C Sharp Tricks - First Post
In this website you will find tips, tricks, gotchas and some recipes for windows as well as web applications. This site is intended to share some of the things I've developed, some source code, articles and some samples I've wrote. Please leave your comments to improve. Thanks and I hope you will find them as useful as i do - Rajesh Lal.
Along with C sharp you will also find the way it interacts with the related technologies.
Along with C sharp you will also find the way it interacts with the related technologies.
- Windows Vista
- ASP.Net
- Microsoft SQL Server
- Javascript, AJAX, Atlas
- Web 2.0 , CSS
- Installation / deployment / orca
- Mobile devices etc
- C# foundation - data structures etc
Subscribe to:
Posts (Atom)