Click here to Skip to main content
15,894,405 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Can anyone give me the code to check number of different word in a text box?

thanks you in advance
Posted
Comments
Maciej Los 12-Feb-13 6:58am    
Please, provide more details. What you mean "different word", do you want to count "and", "or", special characters like: ".", ","?
code_power 12-Feb-13 7:02am    
i am a boy

number of different word = 4
number of similar word = 0
Maciej Los 12-Feb-13 7:15am    
similar words... How much similar: 100%, 75%, any?

If you are after a straight word count, then the simplest method is to use a regex:
C#
string s = File.ReadAllText(@"D:\Temp\MyText.Txt");
Regex wordCount = new Regex("\\w+");
int count = wordCount.Matches(s).Count;
It's not necessarily that efficient, but it should be reasonably accurate.

If you need to evaluate the number of unique words (i.e. count each word once) then you have to do more:
C#
string s = File.ReadAllText(@"D:\Temp\MyText.Txt").ToLower();
string[] words = s.Split(" .,();:?!\r\n".ToArray(), StringSplitOptions.RemoveEmptyEntries);
var dwords = words.Distinct();
int dcount = dwords.Count();
 
Share this answer
 
Comments
Maciej Los 12-Feb-13 7:38am    
OriginalGriff, have a look at my comment to the question. OP wants to count different and similar words. Your answer is perfect for unique words, but not similar. Anyway, +5!
Hi,

the simplest solution, that you can try is, count based on space. you may check for any other symbol also.
C#
int count = (txtbox1.Text.Trim().Split(' ')).Length;

This will give the no. of words. this also count subsequent spaces as a word. so, those stuff you can handle using for loop and increment/decrement the count accordingly.

hope it helps.
 
Share this answer
 
Hi,
I have written a program to take the input from a TextBox and shows the count in message box
C#
private void button2_Click(object sender, EventArgs e)
        {
            List<string> samewords = new List<string>();
            int samewordCnt=0;
            int diffCnt = 0;

            bool found = false;
            bool found1 = false;
            string[] actualWords = textBox1.Text.Split(' ');

            string[] Temp = actualWords;

            for (int iCnt = 0; iCnt < actualWords.Length; iCnt++)
            {
                found = false;
                for (int j = iCnt+1; j < actualWords.Length; j++)
                {
                    if (string.Compare(actualWords[iCnt], actualWords[j], true)==0)
                    {
                        found1 = false;
                        foreach (string tmp in samewords)
                        {
                            if ((string.Compare(tmp, actualWords[j], true)==0))
                            {
                                found1 = true;
                                break;
                            }
                        }
                        if (!found1)
                        {
                            samewords.Add(actualWords[j]);
                            samewordCnt++;
                        }
                        found = true;
                        break;
                    }
                }

                if (!found)
                {
                    found1 = false;
                    foreach (string tmp in samewords)
                    {
                        if ((string.Compare(tmp, actualWords[iCnt], true) == 0))
                        {
                            found1 = true;
                            break;
                        }
                    }
                    if (!found1)
                    {
                        diffCnt++;
                    }
                  
                }

            }

            MessageBox.Show("Same word count "+ samewordCnt.ToString());
            MessageBox.Show("Different word count " + diffCnt.ToString());
           

        }


Best Regards
Muthuraja
 
Share this answer
 
v2

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