13,042,630 members (78,947 online)
Rate this:
See more:
Input should be of any string .. For example, "A peacock is a bird"
Output Should display as..

a - 2,
peacock - 1,
is - 1,
bird - 1

```public void CountIndividualWords()
{
string strInput = "A peacock is a bird";
string[] strWord = strInput.ToString().ToLower().Split(' ');
for (int i = 0; i < strWord.Length; i++)
{

}

}```

Posted 14-Nov-11 1:10am
Updated 14-Nov-11 1:14am
v2
Pandya Anil 14-Nov-11 7:15am

why a = 2, and rest are 1 ?
Mekala@4289 14-Nov-11 7:17am

"a" is repeated twice
LanFanNinja 14-Nov-11 7:59am

Check my solution (solution 5) for an alternate method that is short and simple.

Rate this:

## Solution 1

Take a look at my article here : hOOt - full text search engine[^]

I have a function that extracts word statistics.
thatraja 14-Nov-11 7:22am

Nice article, 5!
Amir Mahfoozi 14-Nov-11 7:56am

Mehdi Gholam 14-Nov-11 8:51am

Thanks
SAKryukov 14-Nov-11 14:35pm

Well, an apparent overkill, unlikely could be used by OP, but I can vote 5 for a good overkill. :-)
--SA
Mehdi Gholam 15-Nov-11 11:26am

Hehe, thanks
Rate this:

## Solution 2

```static void CountWordOccurrence()
{
//tell the user to enter their sentence
Console.WriteLine("\nPlease enter the sentence to be tested.");
//split their input into a string array
//create our SortedList for holding our words and their count
SortedList wordList = new SortedList();
//variable to hold how many times a word appears in the string
int numWords = 0;
//now we loop through the string array
foreach (string word in words)
{
//check and see if this word is in our list yet
if (!(wordList.ContainsKey(word)))
{
//it isnt there so we add it with a value of 1
//since it's the words first occurrence
//increment our word counter
numWords++;
}
else
{
//since the word dous exist we get the count of times it exists
int iWordCount = (int)wordList[word];
//we then increment the count for that word in the list
wordList[word] = iWordCount + 1;
}
}
//now we need an enumerator so we can traverse the list. For
//this we will use the IDictionaryEnumerator and assign that the
//value of the GetEnumerator method of our Sorted List
IDictionaryEnumerator enumerator = wordList.GetEnumerator();
//now we use the MoveNext method of our enumerator
//to tell us whether we're at the end of our list
while (enumerator.MoveNext())
{
//write a blank line
Console.WriteLine();
//now write our the occurrence of each word in the sentence using
//the enumerator.Key (the word) and the enumerator.Value (the count)
Console.WriteLine(string.Format("Word {0} Appeared {1} Times", enumerator.Key, enumerator.Value));
Console.WriteLine();
}
}```

http://www.dreamincode.net/code/snippet2682.htm[^]

v3
thatraja 14-Nov-11 7:22am

5!
Pandya Anil 14-Nov-11 7:25am

tnx
Mehdi Gholam 14-Nov-11 7:33am

I used a dictionary but sorted list is good too, 5'ed

( the < tags are missing )
SAKryukov 14-Nov-11 14:45pm

Sorry, no, I did not, but voted anyway.
--SA
SAKryukov 14-Nov-11 14:45pm

Correct, a 5. I credited your solution in my generalized solution, please see.
--SA
SAKryukov 14-Nov-11 14:47pm

Agree, but I generalized the solution, please see mine.
--SA
Rate this:

## Solution 5

There are many many ways to do this. I whipped this up in about 5 minutes it is a similar but different approach to the solutions posted.

```Dictionary<string,int> occurrences = new Dictionary<string,int>();
string strInput = "A peacock is a bird";
string[] strWord = strInput.ToString().ToLower().Split(' ');
for (int i = 0; i < strWord.Length; i++)
{
if (!occurrences.ContainsKey(strWord[i]))
{
int n = 0;
int result = strWord.Count<string>(
delegate { return strWord[i] == strWord[n++]; });
}
}```

and the code I used to view the results

```for (int i = 0; i < occurrences.Count; i++)
{
MessageBox.Show(occurrences.Keys.ElementAt(i) + " - " +
occurrences.Values.ElementAt(i));
}```
v6
USABebopKid 14-Nov-11 9:50am

My 5.
LanFanNinja 14-Nov-11 9:51am

Thanks! :)
SAKryukov 14-Nov-11 14:47pm

Correct, a 5. I credited your solution in my generalized solution, please see.
--SA
LanFanNinja 14-Nov-11 23:23pm

Thank you!
Rate this:

## Solution 6

you can use this code also, try it , using LAMDA expression

```string str = "A peacock is A bird";
string[] strs = str.ToUpper().Split(' ');
int val ;
foreach (string s in strs)
{
val = 0;
val = strs.Count(x => x.Equals(s));
Console.WriteLine(s + "     " + val.ToString());
}
LanFanNinja 14-Nov-11 23:24pm

+5 Nice! I did not think to use a lamda.
Rate this:

## Solution 4

Top Experts
Last 24hrsThis month
 Karthik Bangalore 130 Satya Prakash Swain 130 OriginalGriff 105 Richard MacCutchan 100 ppolymorphe 85
 OriginalGriff 4,215 RickZeeland 1,834 F-ES Sitecore 1,483 ppolymorphe 1,454 Dave Kreskowiak 1,309