Question

I am not sure how to go about this. Right now I am counting the spaces to get the word count of my string but if there is a double space the word count will be inaccurate. Is there a better way to do this?

Was it helpful?

Solution

Alternate Version of @Martin v. Löwis, which uses a foreach and char.IsWhiteSpace() which should be more correct when dealing with other cultures.

int CountWithForeach(string para)
{
    bool inWord = false;
    int words = 0;
    foreach (char c in para)
    {
        if (char.IsWhiteSpace(c))
        {
            if( inWord )
                words++;
            inWord = false;
            continue;
        }
        inWord = true;
    }
    if( inWord )
        words++;

    return words;
}

OTHER TIPS

While solutions based on Split are short to write, they might get expensive, as all the string objects need to be created and then thrown away. I would expect that an explicit algorithm such as

  static int CountWords(string s)
  {
    int words = 0;
    bool inword = false;
    for(int i=0; i < s.Length; i++) {
      switch(s[i]) {
      case ' ':case '\t':case '\r':case '\n':
          if(inword)words++;
          inword = false;
          break;
      default:
          inword = true;
          break;
      }
    }
    if(inword)words++;
    return words;
  }

is more efficient (plus it can also consider additional whitespace characters).

This seems to work for me:

var input = "This is a  test";
var count = input.Split(" ".ToCharArray(), StringSplitOptions.RemoveEmptyEntries).Length;

Try string.Split:

string sentence = "This     is a sentence     with  some spaces.";
string[] words = sentence.Split(new char[] { ' ' },  StringSplitOptions.RemoveEmptyEntries);
int wordCount = words.Length;
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top