In: Computer Science
C#
Write a console application that takes the following passage and removes every instance of the word "not" using StringBuilder and prints the result out to the console:
I do not like them
In a house.
I do not like them
With a mouse.
I do not like them
Here or there.
I do not like them
Anywhere.
I do not like green eggs and ham.
I do not like them, Sam-I-am.
Ensure that the resulting output reads normally, in other words, it must maintain the same line breaks and not include double-spaces where there should only be a single space. The output should be identical to this:
I do like them
In a house.
I do like them
With a mouse.
I do like them
Here or there.
I do like them
Anywhere.
I do like green eggs and ham.
I do like them, Sam-I-am.
CODE -
using System;
using System.Text;
namespace stringBuilders{
class Sample{
static void Main()
{
// Create StringBuilder object
StringBuilder s = new StringBuilder("I do not like them\nIn a house.\nI do not like them\nWith a mouse.\nI do not like them\nHere or there.\nI do not like them\nAnywhere.\nI do not like green eggs and ham.\nI do not like them, Sam-I-am.");
// Replace "not " with blank to remove not from the passage.
s.Replace("not ", "");
// Display the updated passage.
Console.WriteLine(s);
}
}
}
SCREENSHOT -
If you have any doubt regarding the solution, then do
comment.
Do upvote.