Counting Words with a list of string[] or string?

79 Views Asked by At

Im trying to counting the Words in a File, either i do a list of string[] and get an error at taking out the Whitespaces or i do normal Strings and get an Error in the Splitting String section, also i want to display the three most repeatet Words thats why i need to have a List of all Strings.

Heres the Code:

//Reading File

var path = @"D:\Projects\C sharp Course\Working_with_Files\Working_with_Files_1\Text.txt";
List<string> list = new List<string>();
var content = File.ReadAllText(path);
var text = content.Trim();
string[] splitted;

//Splitting String

for (int i = 0; i < text.Length; i++)
{

    splitted = text.Split(',', ' ', '.', '(', ')');          
    list.Add(splitted);
}

//Taking out Whitespaces

for (int i = 0; i < list.Count; i++)
{
    if (string.IsNullOrWhiteSpace(list[i]))
    {
        list.RemoveAt(i);
    }
}
2

There are 2 best solutions below

2
Tim Schmelter On BEST ANSWER

For every letter in that text file you will add all words. You don't need the for-loop. You also don't need the second loop because String.Split has an overload for that:

char[] splitChars = {',', ' ', '.', '(', ')'};
string[] splitted = text.Split(splitChars, StringSplitOptions.RemoveEmptyEntries); 

The subtask to get the three most repeating words:

string[] top3RepeatingWords = splitted   
   .Groupby(w => w)
   .OrderByDescending(g => g.Count())
   .Select(g => g.Key)
   .Take(3)
   .ToArray();
0
Carra On

Your loop doesn't seem to make sense as each loop will do the same:

for (int i = 0; i < text.Length; i++)//You do not use this i!
{
    splitted = text.Split(',', ' ', '.', '(', ')');          
    list.Add(splitted);//Will add the same splitted text each time.
}

I think you can remove the loop as the Split will already split your entire text.

string[] splitted = text.Split(',', ' ', '.', '(', ')');