I was fed up, so here it is in c#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace CodeChallenge
{
class Program
{
private static List<string> wordsToRemove = new List<string>() { "dog", "cat" };
static void Main(string\[\] args)
{
string text = "dog cat monkey dog horse dog";
string\[\] words = text.Split(' ');
string\[\] output;
int start = 0;
int end = 0;
// test the start
if (wordsToRemove.Contains(words\[0\]) && words.Length > 0)
start = 1;
// test the end
if (wordsToRemove.Contains(words\[words.Length - 1\]) && words.Length != start)
end = (words.Length - 1) - start;
else
end = words.Length - start;
//build
output = new string\[end\];
Array.Copy(words, start, output, 0, end);
//output
Console.WriteLine(string.Join(" ",output));
}
}
}