-1

This Unit Test

[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 .")]

enter image description here

public static void RemoveWordsFromContentAndWrite(StreamReader contentReader, StreamReader wordsReader, StreamWriter outputWriter)
        {
            // TODO #5-5. Implement the method by reading the content and words, removing words from the content, and writing the updated content to the outputWriter. Use StreamReader.Peek method for checking whether there are more characters in the underlying string.
            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();
            }
        }
Rook
  • 5,734
  • 3
  • 34
  • 43
  • What do you mean by "accepting"? It's really unclear what you are asking. – Kirk Woll May 20 '22 at 17:05
  • 1
    Please clarify your specific problem or provide additional details to highlight exactly what you need. As it's currently written, it's hard to tell exactly what you're asking. – CyanCoding May 20 '22 at 17:07

1 Answers1

1

Fairly obvious logic error that you'd have spotted if you'd printed out the value of words and thought for a bit. Remeber that ReadToEnd returns the contents of the stream as a single string.

The result of the unit test shows that the word "et" has been removed from the string. You're just doing a simple substring check on words, which indeed contains the characters "et" next to each other (as part of "amet").

What you probably wanted to do was to check for whole words. There are various ways you can do that, but a trivial change would be this:

var words = wordsReader.ReadToEnd().Split('\n');

String.Split returns an array of strings. LINQ provides you with an IEnumerable<T>.Contains method that does the whole-word search you were reaching for, so if you have a using Linq; directive in your code then the rest of your method works as-is.


Do note though that there are a number of other issues with your code. This isn't the place for code review, but an important one to remember is that the thing you've called bytes is not actually a count of bytes at all, but a count of UTF-16 characters and each character is two bytes wide. Confusing bytes and characters will not end well for your future code!

Rook
  • 5,734
  • 3
  • 34
  • 43