-2

My task is creating method RemoveWordsFromContentAndWrite(StreamReader contentReader, StreamReader wordsReader, StreamWriter outputWriter) Description for my task with my study: The RemoveWordsFromContentAndWrite method should read the content from streamReader and then read words one by one from wordsReader, remove the words from the content, and write the updated content to outputWriter.

Use the StreamReader.Peek method for checking whether there are more characters in the underlying string. Store the content from contentReader to the StringBuilder instance, and use the StringBuilder.Replace method to remove words from the content string.

Example of the string StreamReader consumes:

"Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua."

The word list string is as follows: "ipsumaliquased" For the string above, the RemoveWordsFromContentAndWrite method should write this text to the output stream:

"Lorem dolor sit amet, consectetur adipiscing elit, do eiusmod tempor incididunt ut labore et dolore magna ."

My solution, which is not good enough

        public static void RemoveWordsFromContentAndWrite(StreamReader contentReader, StreamReader wordsReader, StreamWriter outputWriter)
        {
            while (contentReader.Peek() != -1)
            {
                var content = contentReader.ReadToEnd().Split(" ");
                var words = wordsReader.ReadToEnd().Split(" ");
                for (int i = 0; i < content.Length; i++)
                {
                    for (int j = 0; j < words.Length; j++)
                    {
                        if (content[i].Equals(words[j], StringComparison.Ordinal))
                        {
                            content.ToList().Remove(words[j]);
                        }
                    }
                }

                foreach (var item in content)
                {
                    if (item.Equals(content.LastOrDefault(), StringComparison.Ordinal))
                    {
                        outputWriter.Write(item);
                        outputWriter.Flush();
                    }
                    else
                    {
                        outputWriter.Write(item + " ");
                        outputWriter.Flush();
                    }
                }
            }
        }

There are unit tests which unfortunately not passed

        [TestCase("Lorem ipsum dolor sit amet", "ipsum", ExpectedResult = "Lorem  dolor sit amet")]
        [TestCase("Lorem ipsum dolor sit amet", "ipsum\namet", ExpectedResult = "Lorem  dolor sit ")]
        [TestCase("Lorem ipsum dolor sit amet", "ippsum", ExpectedResult = "Lorem ipsum dolor sit amet")]
        [TestCase("Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.", "ipsum", ExpectedResult = "Lorem  dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.")]
        [TestCase("Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.", "ipsum\naliqua", ExpectedResult = "Lorem  dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna .")]
        [TestCase("Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.", "ipsum\naliqua\nsed", ExpectedResult = "Lorem  dolor sit amet, consectetur adipiscing elit,  do eiusmod tempor incididunt ut labore et dolore magna .")]
        [TestCase("Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.", "ipsum\namet\neuysmod\nlabore\naliqua", ExpectedResult = "Lorem  dolor sit , consectetur adipiscing elit, sed do eiusmod tempor incididunt ut  et dolore magna .")]
        [TestCase("Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.", "Lorem\nipsum\namet\neuysmod\nlabore\naliqua\ntempor\nsit\nsed\ndolore\ndolor\nconsectetur\nmagna\nadipiscing\nelit\neiusmod\nincididunt\ndo\net\nut", ExpectedResult = "    ,   ,           .")]
        public string RemoveWordsFromContentAndWrite_ArgumentsAreValid_WritesToStreamWriter(string content, string words)
        {
            // Arrange
            var contentBytes = Encoding.ASCII.GetBytes(content);
            using var contentStream = new MemoryStream(contentBytes);
            using var contentReader = new StreamReader(contentStream);

            var wordsBytes = Encoding.ASCII.GetBytes(words);
            using var wordsStream = new MemoryStream(wordsBytes);
            using var wordsReader = new StreamReader(wordsStream);

            using var outputStream = new MemoryStream();
            using var outputWriter = new StreamWriter(outputStream)
            {
                AutoFlush = true,
            };

          // Act
          WritingToStream.RemoveWordsFromContentAndWrite(contentReader, 
          wordsReader, outputWriter);

            // Assert
            outputStream.Seek(0, SeekOrigin.Begin);
            using var outputReader = new StreamReader(outputStream);
            string updatedContent = outputReader.ReadToEnd();

            return updatedContent;
        }

I would be grateful for every help.

  • 1
    Welcome to Stack Overflow! In what way is your code not working as expected? Please elaborate on the specific problem you are observing and what debugging you have done. To learn more about this community and how we can help you, please start with the [tour] and read [ask] and its linked resources. – David May 16 '22 at 20:56
  • @David I have updated my question. My code is not good enough, because my unit tests are not passed. – MMKKKKKKKMAKSM May 16 '22 at 22:43
  • 2
    Now is the time for you to [do some debugging](https://stackoverflow.com/q/25385173/328193) and [narrow down the problem](https://ericlippert.com/2014/03/05/how-to-debug-small-programs/). Stack Overflow is not a replacement for a debugger. What specifically is failing in the test? When you step through the code in a debugger, which operation produces an unexpected result? What were the values used at the time? What was the result? What result was expected? Why? – David May 16 '22 at 22:53

2 Answers2

0

Here is a piece of code which does what you described:

        public static void RemoveWordsFromContentAndWrite(StreamReader contentReader, StreamReader wordsReader, StreamWriter outputWriter)
    {
        var words = wordsReader.ReadToEnd();
        var bufferSize = 100;
        var buffer = new char[bufferSize];
        var bytesCount = 0;
        var currentWord = new StringBuilder();

        while ((bytesCount = contentReader.ReadBlock(buffer, 0, bufferSize)) > 0)
        {
            for (var i = 0; i < bytesCount; i++)
            {
                if (Char.IsLetterOrDigit(buffer[i]))
                {
                    currentWord.Append(buffer[i]);
                }
                else
                {
                    if (!words.Contains(currentWord.ToString()))
                    {
                        outputWriter.Write(currentWord);
                    }
                    outputWriter.Write(buffer[i]);
                    currentWord.Clear();
                }
            }
            outputWriter.Flush();
        }

        if (!string.IsNullOrEmpty(currentWord.ToString()) && !words.Contains(currentWord.ToString()))
        {
            outputWriter.Write(currentWord);
            outputWriter.Flush();
        }
    }

The above logic does not utilize the hinted methods to obtain the results. I agree with @David's comment that you should put your own work to it, so hopefully this will guide you towards the desired solution.

SzybkiDanny
  • 204
  • 2
  • 11
  • Thank you for your help, your solution is good for my unit tests. I have already understood the way of creating this method. – MMKKKKKKKMAKSM May 17 '22 at 00:37
0
public static void RemoveWordsFromContentAndWrite(StreamReader contentReader, StreamReader wordsReader, StreamWriter outputWriter)
    {
        StringBuilder stringBuilder = new StringBuilder(contentReader.ReadToEnd());
        string[] allWords = wordsReader.ReadToEnd().Split("\n");

        foreach (string word in allWords)
        {
            stringBuilder.Replace(word, string.Empty);
        }

        outputWriter.Write(stringBuilder);
    }
  • Remember that Stack Overflow isn't just intended to solve the immediate problem, but also to help future readers find solutions to similar problems, which requires understanding the underlying code. This is especially important for members of our community who are beginners, and not familiar with the syntax. Given that, **can you [edit] your answer to include an explanation of what you're doing** and why you believe it is the best approach? – Jeremy Caney Jul 16 '22 at 19:38