Monday, December 25, 2006

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"


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


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


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"


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


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

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;
}
}