Click here to Skip to main content
15,887,135 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hello
i read text file that has many words the content of file is saved in s0
and i want to find how many words it has
i used
MyWord = s0.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
MyWord is array of strings
help

What I have tried:

StreamReader sr;
        string[] MyWord = new string[4096];

        private void FormLoad(object sender, EventArgs e)
        { 
            int i, j, count=0;
for (i = 0; i < 4096; i++) MyWord[i] = "";// string.Empty;
            string s0 = sr.ReadToEnd();
            MyWord = s0.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
            sr = new StreamReader(fsr, System.Text.Encoding.Default);
            count = 0;
            for (i = 0; i < 4096; i++)
            {
                if (MyWord[i] != "")//'type of error =Index was outside the bounds of the array.'

                {
                    MessageBox.Show("MyWord["+i.ToString()+"]= "+MyWord[i],"count="+count.ToString());
                    count++;
                }
                else break;
            }
           MessageBox.Show(count.ToString() , "count");
        }
Posted
Updated 4-Feb-24 4:24am
v4

Here is one way of doing it:
C#
using System.Text.RegularExpressions;

string text = "The quick brown, and lazy, fox jumped over the tall-green fence!\r\n What a sight it was. ";

int WordCount = Regex.Matches(text, @"\b[A-Za-z0-9]+\b").Count;
int ParagraphCount = Regex.Matches(text, @"[^\r\n]*[^ \r\n]+[^\r\n]*((\r|\n|\r\n)[^\r\n]*[^ \r\n]+[^\r\n]*)*").Count + 1;

Console.WriteLine(text);
Console.WriteLine($"contains {WordCount} words and {ParagraphCount} paragraphs.");

Outputs:
The quick brown, and lazy, fox jumped over the tall-green fence!
 What a sight it was.
contains 17 words and 2 paragraphs.
 
Share this answer
 
Comments
Engineer khalid 4-Feb-24 10:41am    
i added 3.1415 as a word the count increased by two not by one
may be i need to add some thing to
int WordCount = Regex.Matches(text, @"\b[A-Za-z0-9]+\b").Count;
the above line seems to ask for all words from A to Z and From a to z and numbers from 0 to 9
how can i cover float numers , number that start by + or minuse signe (i almost forgot most of pure c language)
A simpler regex would be
RegEx
\w+
Which would give you
The
quick
brown
and
lazy
fox
jumped
over
the
tall
green
fence
What
a
sight
it
was
Counting the matches would give you the number of words.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900