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

4.0 Given the text file UAH_sample.txt (Located on the Data Canvas Page) write a

ID: 3881563 • Letter: 4

Question

4.0 Given the text file UAH_sample.txt (Located on the Data Canvas Page) write a program that performs the following tasks:

Count the number of words in the file. (Ignore special characters like dash -) Print the result to the screen.

Replace each occurrence of UAH with UAHuntsville. Create a new file with the updated text.

Upload a single MS Word document containing the source code and screenshots showing the output. Also upload the txt files created by problems 1, 2 and 4. (These can be separate files)

Explanation / Answer

Hi,

Here is the program written in C#.

You have to change the file path in the program. It should point to the existing file.

Replacing the string with a custom static extension method to avoid incase sensitivity and avoid usage of Regex. All the options are specified, but commented. Please go through the comments. Copy it to some C# editor.

using System;

using System.IO;

namespace ConsoleApp1

{

    class Program

    {

        static void Main(string[] args)

        {

            // Reading the file from D drive to a string variable called line. Can give                  your file path here

            string line = File.ReadAllText("D:\UAH_sample.txt");

            /*

             * Splitting the string to an array based on space.

             * eg : I am a good boy. word[0] will be 'I'. Word[1] will be 'am'...

                So length of array will give the no of words in the string

            */

            String[] words = line.Split(' ');

            // Displaying the length of array. ie the count of words

            Console.WriteLine(" Number of words in the file - " + words.Length + " ");

            // Can do it in a single line

            //Console.WriteLine(line.Split(' ').Length);

            string strToReplace = "";

            string replaceWith = "";

            // Uncomment the following 4 lines and comment line 32 and 33 if you want to give the input dynamically

            //Console.WriteLine(" Enter the string to replace ");

            //strToReplace = Console.ReadLine(); // String to replace

            //Console.WriteLine(" Replace with ");

            //replaceWith = Console.ReadLine();

            strToReplace = "UAH";

            replaceWith = "UAHuntsville";

            //option 1 - not case sensitive

            // replacing the mentioned string with new one

            //line = line.Replace(strToReplace, replaceWith);

            // option 2

            //replacing with out custom static extension method

            line = StringExtensions.Replace(line, strToReplace, replaceWith);

            //option 3

            // with Regex

            //line = System.Text.RegularExpressions.Regex.Replace(line, strToReplace, replaceWith, System.Text.RegularExpressions.RegexOptions.IgnoreCase);

            Console.WriteLine(line);

            // Create new file nd write the text

            File.WriteAllText("D:\UAH_sample_updated.txt", line);

            //// Displaying number of characters

            //Console.Write(line.Length);

            //// Displaying number of lines

            //Console.WriteLine(line.Split(' ').Length);           

            Console.ReadKey();

        }

    }

    public static class StringExtensions

    {

        public static string Replace(this string originalString, string oldValue, string newValue)

        {

            int startIndex = 0;

            while (true)

            {

                // Checking the word is present or not. Start Index will get the current position of the string

                startIndex = originalString.IndexOf(oldValue, startIndex, StringComparison.CurrentCultureIgnoreCase);

                // Not present

                if (startIndex == -1)

                    break;

                // If Present. Replace the word in the start index ith new Word.

                originalString = originalString.Substring(0, startIndex) + newValue + originalString.Substring(startIndex + oldValue.Length);

                startIndex += newValue.Length;

            }

            return originalString;

        }

    }

}

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