Given a list of strings paragraphs and words. Check each paragraphs's item for containing words's item as a substring. In the case of positive, replace all characters of this substring on 'x'.
I do it in the following way:
// In production, words.Length ~500
var words = new List<string>
{
"technology",
"word",
"IBM",
// ...
};
// In production, paragraphs.Length ~10,000 and paragraphs[i].Length ~200
var paragraphs = new List<string>
{
"Microsoft Corporation is an American multinational technology corporation which produces computer software...",
"Microsoft (the word being a portmanteau of \"microcomputer software\") was founded by Bill Gates and",
"As of 2015, Microsoft is market-dominant in the IBM PC compatible operating system market and the office software",
// ...
};
for (int i = 0; i < paragraphs.Count; i++)
{
foreach (string word in words)
{
var template = new string('x', word.Length);
paragraphs[i] = paragraphs[i].Replace(word, template);
}
}
foreach (string para in paragraphs)
{
Console.WriteLine(para);
}
I think that using algorithms is very bad since there is much String.Replace() calling. As you know, the string type is immutable and each String.Replace() allocates a new space in memory.
Please, review my implementation and help to improve.
