Count Words in a Sentence

Quickly determine the number of words in a sentence by splitting the string.

string sentence = "Count the number of words in this sentence.";
int wordCount = sentence.Split(' ', StringSplitOptions.RemoveEmptyEntries).Length;
Console.WriteLine(wordCount);


Explanation: Using Split with a space delimiter and the RemoveEmptyEntries option ensures accurate word count even with extra spaces.