Read more than one text file and store content in a table

advertisements

i write a c# program that read data from 5 text files and count them according to some given key word

        string[] word_1 = File.ReadAllText(@"C:\Users\Niyomal N\Desktop\Assignment\Assignment\D1_H1.txt").Split(' ');
        string[] word_2 = File.ReadAllText(@"C:\Users\Niyomal N\Desktop\Assignment\Assignment\D2_H1.txt").Split(' ');
        string[] word_3 = File.ReadAllText(@"C:\Users\Niyomal N\Desktop\Assignment\Assignment\D3_H2.txt").Split(' ');
        string[] word_4 = File.ReadAllText(@"C:\Users\Niyomal N\Desktop\Assignment\Assignment\D4_H2.txt").Split(' ');
        string[] word_5 = File.ReadAllText(@"C:\Users\Niyomal N\Desktop\Assignment\Assignment\D5_H2.txt").Split(' ');
        string[] given_doc = File.ReadAllText(@"C:\Users\Niyomal N\Desktop\Assignment\Assignment\Given_doc.txt").Split(' ');

this is how i read from text files, after reading that i use for loop and if loop to count each word from hose file

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

        {

            string s = word_1[i];

                if ("Red".Equals(word_1[i]))
                {
                    //Console.WriteLine(word[i]);

                    h1_r++;
                }
                if ("Green".Equals(word_1[i]))
                {
                    h1_g++;
                }
                if ("Blue".Equals(word_1[i]))
                {
                    h1_b++;
                }

        }

this is the loop i used to get the count from one file and its works fine, i did this process 5 times to read all files, my question is how can i read those 5 files using one for loop and store them in a array (count of each key word)

thanks in advance !!


Copy pasting code is generally not good. It leads to code violating the Don't Repeat Yourself (DRY) rule. Restructure your code:

const string path = @"C:\Users\Niyomal N\Desktop\Assignment\Assignment";
string[] files = new string[] { "D1_H1.txt", "D2_H1.txt", "D3_H1.txt", ... };

foreach (string file in files) {
    string fullPath = Path.Combine(path, file);
    //TODO: count words of file `fullPath`
}

Storing the word counts in an array is not optimal as you will have to traverse the array for each word you are encountering in a file. Use a dictionary instead which has a constant lookup time. That's much faster.

var wordCount = new Dictionary<string, int>();

You can then count the words like this:

int count;
if (wordCount.TryGetValue(word, out count)) {
    wordCount[word] = count + 1;
} else {
    wordCount[word] = 1;
}


UPDATE

You can test for keywords like this

var keywords = new HashSet<string> { "Red", "Green", "Blue" };

string word = "Green";
if (keywords.Contains(word)) {
    ...
}

HasSets are as fast as dictionaries.

Be careful with the word casing. HashSets are case sensitive by default. If "red" and "Red" and "RED" have to be found alltogehter, initialize the HashSet like this:

var keywords = new HashSet<string>(StringComparer.InvariantCultureIgnoreCase)
    { "Red", "Green", "Blue" };