public List <MatchedWord> Match(string[] scrambledWords, string[] wordList) { List <MatchedWord> matchedWords = new List <MatchedWord>(); if (scrambledWords == null) { return(null); } foreach (var scrambledWord in scrambledWords) { foreach (var word in wordList) { if (scrambledWord.Equals(word, StringComparison.OrdinalIgnoreCase)) { matchedWords.Add(BuildMatchedWord(scrambledWord, word)); } else { char[] scrambledWordArray = scrambledWord.ToCharArray(); char[] wordArray = word.ToCharArray(); Array.Sort(scrambledWordArray); Array.Sort(wordArray); string scrambledOrdered = ""; foreach (var chr in scrambledWordArray) { scrambledOrdered += chr.ToString(); } string wordOrdered = ""; foreach (var chr in wordArray) { wordOrdered += chr.ToString(); } if (scrambledOrdered.Equals(wordOrdered, StringComparison.OrdinalIgnoreCase)) { matchedWords.Add(BuildMatchedWord(scrambledWord, word)); } } } } MatchedWord BuildMatchedWord(string scrambledWord, string word) { MatchedWord matchedWord = new MatchedWord { ScrambledWord = scrambledWord, Word = word }; return(matchedWord); } return(matchedWords); }
public List <MatchedWord> Match(string[] scrambledWords, string[] wordList) { List <MatchedWord> matchedWords = new List <MatchedWord>(); //foreach (var scrambledWord in scrambledWords) //{ // foreach (var word in wordList) // { // //scrambled word already matches word // if (scrambledWord.Equals(word, StringComparison.OrdinalIgnoreCase)) // { // matchedWords.Add(BuildMatchedWord(scrambledWord, word)); // } // else // { // //convert strings into char array // char [] scrambledWordArray = scrambledWords.ToCharArray(); // char[] wordListArray = wordList.ToCharArray(); // //sort both character arrays (Array.sort()) // Array.Sort(scrambledWordArray); // Array.Sort(wordListArray); // //act -> sort -> act // //cat -> sort -> act // //compare sorted char arrays or convert to string and compare // scrambledWordArray.ToString(); // wordListArray.ToString(); // foreach (var scrambledWord2 in scrambledWordArray) // { // foreach (var word2 in wordList) // { // //if they are equal, add to matchedWords list // if (scrambledWord2.Equals(word2, StringComparison.OrdinalIgnoreCase)) // { // matchedWords.Add(BuildMatchedWord(scrambledWord2, word2)); // } // } // } // } // } //} MatchedWord BuildMatchedWord(string scrambledWord, string word) { MatchedWord matchedWord = new MatchedWord { ScrambledWorld = scrambledWord, Word = word }; return(matchedWord); } return(matchedWords); }
public List <MatchedWord> Match(string[] scrambledWords, string [] wordList) { List <MatchedWord> matchedWords = new List <MatchedWord>(); foreach (var scrambledWord in scrambledWords) { foreach (var word in wordList) { //scrambledWord already matched word if (scrambledWord.Equals(word, StringComparison.OrdinalIgnoreCase)) { matchedWords.Add(BuildMatchedWord(scrambledWord, word)); } else { //convert strings into chraters arrays char[] scrambledWordArray = word.ToCharArray(); //sort both charater arrays(Array.sort()) Array.Sort(scrambledWordArray); //convert character arrays back to strings string scrambledString = new string(scrambledWordArray); //compare the two strings //if they are equal, add to matchedWord list if (scrambledWord.Equals(scrambledString, StringComparison.OrdinalIgnoreCase)) { matchedWords.Add(BuildMatchedWord(scrambledWord, scrambledString)); } } //end of if } //end of 2nd foraech } //end of 1st foreach MatchedWord BuildMatchedWord(string scrambledWord, string word) { MatchedWord matchedWord = new MatchedWord { ScrambledWord = scrambledWord, Word = word }; return(matchedWord); } return(matchedWords); }
MatchedWord BuildMatchedWord(string scrambledWord, string word) { MatchedWord matchedWord = new MatchedWord { ScrambledWord = scrambledWord, Word = word }; return(matchedWord); }
private MatchedWord BuildMatchedWord(string scrambleword, string word) { var matchedWord = new MatchedWord { ScrambledWord = scrambleword, Word = word }; return(matchedWord); }
public List <MatchedWord> Match(string[] scrambledWords, string[] wordList) { List <MatchedWord> matchedWords = new List <MatchedWord>(); foreach (var scrambledWord in scrambledWords) { foreach (var word in wordList) { //scrambledWord already matches word if (scrambledWord.Equals(word, StringComparison.OrdinalIgnoreCase)) { matchedWords.Add(BuildMatchedWord(scrambledWord, word)); } else { char[] scrambledWordArray = scrambledWord.ToCharArray(); char[] scrambledWordArray2 = word.ToCharArray(); Array.Sort(scrambledWordArray); Array.Sort(scrambledWordArray2); string scramble = new string(scrambledWordArray); string scramble2 = new string(scrambledWordArray2); if (scramble.Equals(scramble2)) { matchedWords.Add(BuildMatchedWord(scrambledWord, word)); } //convert strings into character arrays //sort both character arrays (Array.sort()) //act -> sort -> act //cat -> sort -> act //convert character arrays back to a string //compare the two strings //if they are equal, add to matchedWords list } } } MatchedWord BuildMatchedWord(string scrambledWord, string word) { MatchedWord matchedWord = new MatchedWord { ScrambledWord = scrambledWord, Word = word }; return(matchedWord); } return(matchedWords); }
public List <MatchedWord> Match(string[] scrambledWords, string[] wordList) { List <MatchedWord> matchedWords = new List <MatchedWord>(); foreach (var scrambledWord in scrambledWords) { foreach (var word in wordList) { //scrambledWord already matches word if (scrambledWord.Equals(word, StringComparison.OrdinalIgnoreCase)) { matchedWords.Add(BuildMatchedWord(scrambledWord, word)); } else { //Convert strings into character arrays char[] scrambledWordArray = scrambledWord.ToCharArray(); char[] wordArray = word.ToCharArray(); //Sort both character arrays (Array.sort()) Array.Sort(scrambledWordArray); Array.Sort(wordArray); //Convert sorted character arrays back to strings string sortedScrambledWord = new string(scrambledWordArray); string sortedWord = new string(wordArray); //Compare the two strings if (sortedScrambledWord.Equals(sortedWord, StringComparison.OrdinalIgnoreCase)) { matchedWords.Add(BuildMatchedWord(scrambledWord, word)); } } } } MatchedWord BuildMatchedWord(string scrambledWord, string word) { MatchedWord matchedWord = new MatchedWord { ScrambledWord = scrambledWord, Word = word }; return(matchedWord); } return(matchedWords); }
private static void DisplayMatchedUnscrambledWords(string[] scrambledWords) { //read the list of words in the wordlist.txt file (unscrambled words) string[] wordList = _fileReader.Read("wordlist.txt"); //call a word matcher method, to get a list of MatchedWord structs List <MatchedWord> matchedWords = _wordMatcher.Match(scrambledWords, wordList); //display the match - print to console if (matchedWords.Any()) { //loop through matchedWords and print to console the contents of the structs for (int i = 0; i < matchedWords.Count; i++) { //foreach foreach (var matchedWord in matchedWords) { Console.WriteLine("MATCH FOUND FOR {0} : {1} ", matchedWord.ScrambledWord, matchedWord.Word); //matched found for act: cat //matched found for act: cat } } //write to console //matched found for act: cat } else { //no matches have been found Console.WriteLine(); } MatchedWord BuildMatchedWord(string scrambledWord, string word) { MatchedWord matchedWord = new MatchedWord { ScrambledWord = scrambledWord, Word = word }; return(matchedWord); } }
public List <MatchedWord> Match(string[] scrambledWords, string[] wordList) { List <MatchedWord> matchedWords = new List <MatchedWord>(); foreach (string scrambledWord in scrambledWords) { foreach (string word in wordList) { //scrambledWord already matches word if (scrambledWord.Equals(word, StringComparison.OrdinalIgnoreCase)) { matchedWords.Add(BuilMatchedWord(scrambledWord, word)); } else { //convert the strings into character arrays char[] scrambledWordChar = scrambledWord.ToCharArray(); char[] wordChar = word.ToCharArray(); //sort both character arrays (Array.Sort()) //act -> sort -> act //cat -> sort -> act Array.Sort(scrambledWordChar); Array.Sort(wordChar); //convert arrays back to a string string scrambledWordStr = ""; string wordStr = ""; foreach (char character in scrambledWordChar) { scrambledWordStr += character; } foreach (char character in wordChar) { wordStr += character; } //compare the two strings //if equal, add to List if (scrambledWordStr.Equals(wordStr)) { matchedWords.Add(BuilMatchedWord(scrambledWord, word)); } } } } MatchedWord BuilMatchedWord(string scrambledWord, string word) { MatchedWord matchedWord = new MatchedWord { ScrambledWord = scrambledWord, Word = word }; return(matchedWord); } return(matchedWords); }