/// <summary> /// Create a sorted list of DistinctWords using the Tokens in a Text object /// </summary> /// <param name="txt">The Text object of which we use its Tokens</param> public DistinctWordList(Text txt) { DistinctWords = new List<DistinctWord> ( ); foreach (string s in txt.Tokens) { if (s.IndexOfAny ("!$%&*()-+=}{[]|\n\r\t':;?.,<>~".ToCharArray ( )) == -1) { DistinctWord dw = new DistinctWord (s); int n = DistinctWords.IndexOf (dw); if (n == -1) DistinctWords.Add (dw); else DistinctWords[n].Count++; } } DistinctWords.Sort ( ); }
/// <summary> /// Displays the contents of wordList alphabetically. /// </summary> /// <returns>string containing contents of wordList</returns> public string DisplayAlphabetically() { //the string that will be returned with the header that will display what is in what //column using the string format method string result = string.Format("\n\n\t{0,-20} {1,-20}", "Word", "Count" + "\n\t-------- ---------\n"); int n = 1; //for every word in the list min is i for (int i = 0; i < wordList.Count - 1; ++i) { int min = i; //for word j in the list compare to i for (int j = i + 1; j < wordList.Count; ++j) { //if word j is smaller than i then the min is now j if (wordList[j].CompareTo(wordList[min]) < 0) { min = j; } } //temporary DistinctWord DistinctWord temp = wordList[i]; //add word to wordList wordList[i] = wordList[min]; wordList[min] = temp; } //format the words for display foreach (DistinctWord word in wordList) { result += "\n\t" + n + ". " + word; n++; } return(result); }