Hi,
Please try the following code:
public int GetNoOfWords(string s)
{
return s.Split(new char[] { ' ', '.', ',','?' }, StringSplitOptions.RemoveEmptyEntries).Length;
}
The above code assumes space, comma, full stop, question mark as word separators and will return the word count.
And call this function like this:
string text = "This is an simple example which demonstrates how to count the number words in a string using C#. This is a test string.Hence, Test it properly in c#.";
int words = GetNoOfWords(text)
Check where you get the count to be maximum.
Hope this helps !!
Regards,
Praneet