15,843,865 members
1.00/5 (1 vote)
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

Can anyone please help me out in code(C#) or some idea?

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

}

}```

Thanks in advance,
Posted
Updated 14-Nov-11 2:14am
v2
Comments
Pandya Anil 14-Nov-11 7:15am
why a = 2, and rest are 1 ?
mibi@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.

## Solution 1

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

I have a function that extracts word statistics.

Comments
thatraja 14-Nov-11 7:22am
Nice article, 5!
See my reply there
Amir Mahfoozi 14-Nov-11 7:56am
+5 for your beneficial article
Mehdi Gholam 14-Nov-11 8:51am
Thanks
Sergey Alexandrovich Kryukov 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

## 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
string[] words = Console.ReadLine().Split(' ');
//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
wordList.Add(word, 1);
//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();
}
Console.ReadKey(true);
}```

the answer is from here....
http://www.dreamincode.net/code/snippet2682.htm[^]

mark as answer if solved your problem... it motivates :)

v3
Comments
thatraja 14-Nov-11 7:22am
5!
Pandya Anil 14-Nov-11 7:25am
tnx
Sergey Alexandrovich Kryukov 14-Nov-11 14:47pm
Agree, but I generalized the solution, please see mine.
--SA
Mehdi Gholam 14-Nov-11 7:33am
I used a dictionary but sorted list is good too, 5'ed

( the < tags are missing )
Sergey Alexandrovich Kryukov 14-Nov-11 14:45pm
<scratch>I credited your solution in my generalized solution, too, please see.
Sorry, no, I did not, but voted anyway.
--SA

## 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.

C#
```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++]; });
occurrences.Add(strWord[i], result);
}
}```

and the code I used to view the results

C#
```for (int i = 0; i < occurrences.Count; i++)
{
MessageBox.Show(occurrences.Keys.ElementAt(i) + " - " +
occurrences.Values.ElementAt(i));
}```

v6
Comments
ARBebopKid 14-Nov-11 9:50am
My 5.
LanFanNinja 14-Nov-11 9:51am
Thanks! :)
Sergey Alexandrovich Kryukov 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!

## Solution 6

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

C#
```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());
}
Console.ReadLine();```

Comments
LanFanNinja 14-Nov-11 23:24pm
+5 Nice! I did not think to use a lamda.

## Solution 4

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

Top Experts
Last 24hrsThis month
 OriginalGriff 135 Patrice T 110 CPallini 105 Pete O'Hanlon 75 0x01AA 50
 OriginalGriff 1,778 Richard Deeming 705 Dave Kreskowiak 561 Graeme_Grant 479 Patrice T 285

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