Split the string into words. If the words are always separated by single spaces, you can use
String.Split[
^]; otherwise, you might need to use a regular expression:
string[] words = System.Text.RegularExpressions.Regex.Matches(input, @"\w+")
.Cast<System.Text.RegularExpressions.Match>()
.Select(m => m.Value)
.ToArray();
Regular Expression Language - Quick Reference | Microsoft Docs[
^]
Then you need to generate all combinations of words. Eric Lippert has a nice series of blog posts explaining how to do that:
Producing combinations, part one | Fabulous adventures in coding[
^]
Or you can look at the implementations on Rosetta Code:
Combinations - Rosetta Code[
^]
A simple version might look something like this:
static IEnumerable<IReadOnlyList<T>> AllCombinations<T>(IReadOnlyList<T> input)
{
for (int m = input.Count; m >= 1; m--)
{
var result = new T[m];
var stack = new Stack<int>();
stack.Push(0);
while (stack.Count > 0)
{
int index = stack.Count - 1;
int value = stack.Pop();
while (value < input.Count)
{
result[index] = input[value];
index++;
value++;
stack.Push(value);
if (index == m)
{
yield return result;
break;
}
}
}
}
}
You then combine the two:
IEnumerable<string> parts = AllCombinations(words).Select(c => string.Join(" AND ", c));
string result = "(" + string.Join(") OR (", parts) + ")";