You can try regular expressions in order to match required chunks:
using System.Linq;
using System.Text.RegularExpressions;
public static string Decode(string morseCode) {
string[] words = Regex.Matches(morseCode, @"(?<=^|\s).+?(?=$|\s)")
.Cast<Match>()
.Select(match => match.Value.All(c => char.IsWhiteSpace(c))
? match.Value
: match.Value.Trim())
.ToArray();
//Relevant code here
}
Demo:
using System.Linq;
using System.Text.RegularExpressions;
...
string morseCode = ".... . -.-- .--- ..- -.. .";
string[] words = Regex.Matches(morseCode, @"(?<=^|\s).+?(?=$|\s)")
.Cast<Match>()
.Select(match => match.Value.All(c => char.IsWhiteSpace(c))
? match.Value
: match.Value.Trim())
.ToArray();
string report = string.Join(Environment.NewLine, words
.Select((word, i) => $"words[{i}] = \"{word}\""));
Console.Write(report);
Outcome:
words[0] = "...."
words[1] = "."
words[2] = "-.--"
words[3] = " "
words[4] = ".---"
words[5] = "..-"
words[6] = "-.."
words[7] = "."