Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write a C# program that reads a text file and counts the total number of words i

ID: 647455 • Letter: W

Question

Write a C# program that reads a text file and counts the total number of words in the file, the total number of spaces in the file, the number of words of each length in the file, and the average word length in the file. You may assume that a word is any set of characters, except digits and punctuation, between two spaces or between a space and the start or end of a line. You should tabulate the length of words from length one to length fifteen. A file called HowIMetMyWife.txt has been placed on the web site for use as a test file in this assignment. Design your own GUI interface for this assignment. At a minimum you should have an easy way for the user to select the file for analysis and an appropriate way to display the results. Your program MUST be modular - that is, it should consist of a main program that is largely a sequence of function calls.

Explanation / Answer

using System.IO;

using System.Linq;

using System.Text;

public static class CountSubstringsCharByChar

{

    static void Main()

    {

        // Read the input list of words

        string[] wordsOriginal = File.ReadAllLines("words.txt");

        string[] wordsLowercase =

            wordsOriginal.Select(w => w.ToLower()).ToArray();

        // Process the file char by char

        int[] occurrences = new int[wordsLowercase.Length];

        StringBuilder buffer = new StringBuilder();

        using (StreamReader text = File.OpenText("text.txt"))

        {

            int nextChar;

            while ((nextChar = text.Read()) != -1)

            {

                char ch = (char)nextChar;

                if (char.IsLetter(ch))

                {

                    // A letter is found --> check all words for matches

                    ch = char.ToLower(ch);

                    buffer.Append(ch);

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

                    {

                      string word = wordsLowercase[i];

                      if (buffer.EndsWith(word))

                      {

                          occurrences[i]++;

                      }

                    }

                }

                else

                {

                    // A non-letter is found --> clean the buffer

                    buffer.Clear();

                }

            }

        }

        // Print the result

        using (StreamWriter result = File.CreateText("result.txt"))

        {

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

            {

                result.WriteLine("{0} --> {1}",

                    wordsOriginal[i], occurrences[i]);

            }

        }

    }

    static bool EndsWith(this StringBuilder buffer, string str)

    {

        if (buffer.Length < str.Length)

        {

            return false;

        }

        for (int bufIndex = buffer.Length-str.Length, strIndex = 0;

            strIndex < str.Length;

            bufIndex++, strIndex++)

        {

            if (buffer[bufIndex] != str[strIndex])

            {

                return false;

            }

        }

        return true;

    }

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote