This method will return the string array of match words
I do need to search the text file contain list of word per line and i want to return list of matching word from that text file.
example for “trainer”, I should get a list equivalent to [“terrain”, “trainer”, “retrain”].
public static string[] GetMatchesFor(string word, string pathToWordsDatabase)
{
string[] Database = File.ReadAllLines(pathToWordsDatabase);
string result = string.Empty;
foreach (string str in Database)
{
bool isMatch = false;
if (str.Length == word.Length)
{
char[] chrArray = word.ToCharArray();
foreach (char chr in chrArray)
{
if (str.Contains(chr.ToString()))
{
isMatch = true;
}
else
{
isMatch = false;
break;
}
}
if (isMatch)
{
result = result + str + ",";
}
}
else
{
continue;
}
}
int count = result.Split(',').Length - 1;
string[] resultArray = new string[count];
for (int i = 0; i < count; i++)
{
resultArray[i] = result.Split(',')[i];
}
return resultArray;
}
public static string[] GetMatchesFor(string word, string pathToWordsDatabase)
{
string[] Database = File.ReadAllLines(pathToWordsDatabase);
string result = string.Empty;
foreach (string str in Database)
{
bool isMatch = false;
if (str.Length == word.Length)
{
char[] chrArray = word.ToCharArray();
foreach (char chr in chrArray)
{
if (str.Contains(chr.ToString()))
{
isMatch = true;
}
else
{
isMatch = false;
break;
}
}
if (isMatch)
{
result = result + str + ",";
}
}
else
{
continue;
}
}
int count = result.Split(',').Length - 1;
string[] resultArray = new string[count];
for (int i = 0; i < count; i++)
{
resultArray[i] = result.Split(',')[i];
}
return resultArray;
}
No comments:
Post a Comment