DarkN3ss
DarkN3ss

Reputation: 75

Count how many words in each sentence

I'm stuck on how to count how many words are in each sentence, an example of this is: string sentence = "hello how are you. I am good. that's good." and have it come out like:

//sentence1: 4 words
//sentence2: 3 words
//sentence3: 2 words

I can get the number of sentences

    public int GetNoOfWords(string s)
    {
        return s.Split(new char[] { '.' }, StringSplitOptions.RemoveEmptyEntries).Length;
    }
    label2.Text = (GetNoOfWords(sentance).ToString());

and i can get the number of words in the whole string

    public int CountWord (string text)
    {
        int count = 0;
        for (int i = 0; i < text.Length; i++)
        {
            if (text[i] != ' ')
            {
                if ((i + 1) == text.Length)
                {
                    count++;
                }
                else
                {
                    if(text[i + 1] == ' ')
                    {
                        count++;
                    }
                }
            } 
        }
        return count;
    }

then button1

        int words = CountWord(sentance);
        label4.Text = (words.ToString());

But I can't count how many words are in each sentence.

Upvotes: 1

Views: 21316

Answers (8)

ppp
ppp

Reputation: 771

If you only need a count, I'd avoid Split() -- it takes up unnecessary space. Perhaps:

static int WordCount(string s)
{
    int wordCount = 0;
    for(int i = 0; i < s.Length - 1; i++)
        if (Char.IsWhiteSpace(s[i]) && !Char.IsWhiteSpace(s[i + 1]) && i > 0)
            wordCount++;
    return ++wordCount;
}

public static void Main()
{
    Console.WriteLine(WordCount(" H elloWor    ld g  ")); // prints "4"
}

It counts based on the number of spaces (1 space = 2 words). Consecutive spaces are ignored.

Upvotes: 2

Bryan Hong
Bryan Hong

Reputation: 1483

Why not use Split instead?

        var sentences = "hello how are you. I am good. that's good.";

        foreach (var sentence in sentences.TrimEnd('.').Split('.'))
            Console.WriteLine(sentence.Trim().Split(' ').Count());

Upvotes: 4

RJ Lohan
RJ Lohan

Reputation: 6527

As noted in several answers here, look at String functions like Split, Trim, Replace, etc to get you going. All answers here will solve your simple example, but here are some sentences which they may fail to analyse correctly;

"Hello, how are you?" (no '.' to parse on)
"That apple costs $1.50."  (a '.' used as a decimal)
"I   like     whitespace    .    "   
"Word"  

Upvotes: 1

Andrew Cooper
Andrew Cooper

Reputation: 32596

string text = "hello how are you. I am good. that's good.";
string[] sentences = s.Split(new char[] { '.' }, StringSplitOptions.RemoveEmptyEntries);
IEnumerable<int> wordsPerSentence = sentences.Select(s => s.Trim().Split(' ').Length);

Upvotes: 1

user1610015
user1610015

Reputation: 6678

Use CountWord on each element of the array returned by s.Split:

string sentence = "hello how are you. I am good. that's good.";
string[] words = sentence.Split(new char[] { '.' }, StringSplitOptions.RemoveEmptyEntries).Length;

for (string sentence in sentences)
{
    int noOfWordsInSentence = CountWord(sentence);
}

Upvotes: 1

site
site

Reputation: 1638

Does your spelling of sentence in:

int words = CountWord(sentance);

have anything to do with it?

Upvotes: -1

gudatcomputers
gudatcomputers

Reputation: 2882

If you want number of words in each sentence, you need to

string s = "This is a sentence. Also this counts. This one is also a thing.";
string[] sentences = s.Split(new char[] { '.' }, StringSplitOptions.RemoveEmptyEntries);
foreach(string sentence in sentences)
{
    Console.WriteLine(sentence.Split(' ').Length + " words in sentence *" + sentence + "*");
}

Upvotes: 1

evanmcdonnal
evanmcdonnal

Reputation: 48134

Instead of looping over the string as you do in CountWords I would just use;

 int words = s.Split(' ').Length;

It's much more clean and simple. You split on white spaces which returns an array of all the words, the length of that array is the number of words in the string.

Upvotes: 6

Related Questions