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


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

Labels: ,

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

Labels: ,



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

Labels: ,

Saturday, November 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);

}

Labels: ,

Wednesday, October 25, 2006



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

}

Labels: ,



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

Labels: ,

Monday, September 25, 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();
}

Labels: ,

Sunday, August 20, 2006