Friday, March 2, 2012

How to use relative paths for an Assembly in the C# projects in order to avoid the conflicts with the references?

Let us consider the following hierarchy of the project



Let us say the working project name is WorkingProject . Follow the following steps in order to use relative paths for the assemblies

1. Add the references to the project (LocalFunctions.dll and LocalClasses.dll) by browsing. To Add reference to a project, right click on the reference folder and click on add reference.

2. Open the physical location of the WorkingProject

3. Edit the .csproj file in notepad++ or notepad.

4. Look for the HintPath tag for each and every reference and check the required relative paths under this tag value.

E.g. in this case it should be like this


a)      ..\..\ LocalSolution \ LocalFunctions \bin\Debug\LocalFunctions.dll
b)      ..\..\ LocalSolution \ LocalClasses\ bin\Debug\ LocalClasses.dll



Note: By default these relative paths will be populated based on the added references w.r.t. current working directly of the project.

If you need any windows assembly use path like

C:\Program Files\Reference Assemblies\Microsoft\Framework\v3.5\System.Xml.Linq.dll



Thursday, February 18, 2010

How to find no of unique characters in a string using in build functions in c#?

static int GetUniqueCharCountWithInBuiltFunc(string inputString)
{
if (string.IsNullOrEmpty(inputString))
{
throw new NullReferenceException();
}
char[] inputArray = inputString.Replace(" ", "").ToCharArray();
return inputArray.Distinct().Count();
}

How to count unique characters in a string using c#?

static int GetCharacterCount(string inputString)
{
Dictionary charactersDictinary = new Dictionary();
if (string.IsNullOrEmpty(inputString))
{
throw new NullReferenceException();
}
for (int i = 0; i < inputString.Length; i++)
{
if (charactersDictinary.ContainsKey(inputString[i]))
{
i++;
}
else
{
charactersDictinary.Add(inputString[i], i);
}

}
return charactersDictinary.Count;
}

How to count the number of words in given string using c#?

static int GetWordCount(string inputString, bool checkCaseSensitivity)
{
Dictionary charactersDictinary = new Dictionary();
if (string.IsNullOrEmpty(inputString))
{
throw new InvalidOperationException();
}
if (!checkCaseSensitivity)
{
inputString = inputString.ToLower();
}
string[] inputStringArray = inputString.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
for (int i = 0; i < inputStringArray.Length; i++)
{
if (charactersDictinary.ContainsKey(inputStringArray[i]))
{
i++;
}
else
{
charactersDictinary.Add(inputStringArray[i], i);
}

}
return charactersDictinary.Count;
}

How to find anagrams from a given list of strings using c#?

public static Dictionary> GetAnagramEquivalents()
{

// "cat", "tac", "army", "pam", "act", "map", "mary", "self"
// "act": "act", "cat", "tac"
// "army": "army", "mary"
// "map": "map", "pam"
// "self": "self"


List InputListStrings;

string[] inputStrings = new string[] {"cat", "tac", "army", "pam", "act", "map", "mary", "self","act", "act", "cat", "tac","army", "army", "mary","map","map", "pam","self","self"};
InputListStrings = inputStrings.ToList();
Dictionary> dictionaryReturnList = new Dictionary>();
for (int x = 0; x < InputListStrings.Count; ++x) { char[] InputCharArray = InputListStrings[x].ToCharArray(); Array.Sort(InputCharArray); string InputString = new string(InputCharArray); //check if the dic contains the key then add value to its corresponding list if (dictionaryReturnList.ContainsKey(InputString)) { dictionaryReturnList[InputString].Add(InputListStrings[x]); } else { dictionaryReturnList.Add(InputString, new List());
dictionaryReturnList[InputString].Add(InputListStrings[x]);
}
}
return dictionaryReturnList;
}

How to check whether one string is anagram of other in c#?

static bool CheckAnagramOfString(string inputString1, string inputString2)
{
char[] charArr1 = inputString1.ToCharArray();
char[] charArr2 = inputString2.ToCharArray();
//sort both of them in same order
Array.Sort(charArr1);
Array.Sort(charArr2);
inputString1 = new string(charArr1);
inputString2 = new string(charArr2);

if (inputString1.Equals(inputString2))
{
return true;
}
else
{
return false;
}

}

How to sort a comma separated array elements from a string using c# with Bubble sort?

static void BubbleSort(string inputString)
{
string[] inputStingArray = inputString.Split(' ');
int[] inputIntArr = inputStingArray.Select(x => int.Parse(x)).ToArray();
if (inputIntArr == null || inputIntArr.Length <= 0) { throw new ArgumentException("Inavlid array input"); } int temp; for (int i = inputIntArr.Length - 1; i >= 0; i--)
{
for (int j = 0; j < i; j++) { if (inputIntArr[j] > inputIntArr[j + 1])
{
temp = inputIntArr[j];
inputIntArr[j] = inputIntArr[j + 1];
inputIntArr[j + 1] = temp;
}
}
}

Console.WriteLine("Each individual Item in Array after sorting is");
foreach (int item in inputIntArr)
{
Console.WriteLine(item);
}
Console.ReadKey();
}

Write a c# code to find the factorial of a function?

static int GetFactorial(string inputValue)
{
int number;
bool IsNumber = true;
IsNumber = Int32.TryParse(inputValue, out number);
if (IsNumber)
{
int result = 1;
int counter = number;
do
{
result = result * counter;
counter--;
} while (counter >= 1);
return result;
//with factorial
//if (number <= 1)
//{
// return 1;
//}
//else
//{
// result = number * GetFactorial((number - 1).ToString());
// return result;
//}
}
else
{
throw new Exception("Input value is not a valid number");
}
}

How to find no of occurences of a string in a file using c#?

static int countWordOccurrencesInFile(string filePath, string searchText)
{
// FileStream fstream = File.Open(filePath,FileMode.Open);
using (StreamReader reader = new StreamReader(filePath))
{
string strContents = reader.ReadToEnd();

//Dictionary uniqueWords = new Dictionary();
int occurrences = 0;
int foundPos = -1;
int startIndex = 0;
do
{
foundPos = strContents.IndexOf(searchText, startIndex);
if (foundPos > -1)
{
startIndex = foundPos + 1;
occurrences++;
}
} while (foundPos > -1);

return occurrences;
}
}

How to find duplicate words in a file using c#?

static List countDuplicateWordsInFile(string filePath)
{
// FileStream fstream = File.Open(filePath,FileMode.Open);
using (StreamReader reader = new StreamReader(filePath))
{
string strContents = reader.ReadToEnd();
List duplicateWords = new List();
Dictionary uniqueWords = new Dictionary();
string[] strArray = strContents.Split(new string[] { " ", "\r\n" }, StringSplitOptions.RemoveEmptyEntries);
for (int i = 0; i < strArray.Length; i++)
{
if (uniqueWords.ContainsKey(strArray[i]))
{
//add duplicate words
duplicateWords.Add(strArray[i]);
}
else
{
//unique words
uniqueWords.Add(strArray[i], i);
}
}

return duplicateWords;
}
}