Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C#2.0 C#3.0 C# C#4.0
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?
 
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 14-Nov-11 2:10am
Edited 14-Nov-11 2:14am
v2
Comments
Pandya Anil at 14-Nov-11 7:15am
   
why a = 2, and rest are 1 ?
Mekala@4289 at 14-Nov-11 7:17am
   
"a" is repeated twice
LanFanNinja at 14-Nov-11 7:59am
   
Check my solution (solution 5) for an alternate method that is short and simple.
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

Take a look at my article here : hOOt - full text search engine[^]
 
I have a function that extracts word statistics.
  Permalink  
Comments
thatraja at 14-Nov-11 7:22am
   
Nice article, 5!
See my reply there
Amir Mahfoozi at 14-Nov-11 7:56am
   
+5 for your beneficial article
Mehdi Gholam at 14-Nov-11 8:51am
   
Thanks
SAKryukov at 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 at 15-Nov-11 11:26am
   
Hehe, thanks
Rate this: bad
good
Please Sign up or sign in to vote.

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 Smile | :)
  Permalink  
v3
Comments
thatraja at 14-Nov-11 7:22am
   
5!
Pandya Anil at 14-Nov-11 7:25am
   
tnx
SAKryukov at 14-Nov-11 14:47pm
   
Agree, but I generalized the solution, please see mine.
--SA
Mehdi Gholam at 14-Nov-11 7:33am
   
I used a dictionary but sorted list is good too, 5'ed
 
( the < tags are missing )
SAKryukov at 14-Nov-11 14:45pm
   
I credited your solution in my generalized solution, too, please see.
Sorry, no, I did not, but voted anyway.
--SA
SAKryukov at 14-Nov-11 14:45pm
   
Correct, a 5. I credited your solution in my generalized solution, please see.
--SA
Rate this: bad
good
Please Sign up or sign in to vote.

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++]; });
        occurrences.Add(strWord[i], result);
    }
}
 
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));
}
  Permalink  
v6
Comments
USABebopKid at 14-Nov-11 9:50am
   
My 5.
LanFanNinja at 14-Nov-11 9:51am
   
Thanks! :)
SAKryukov at 14-Nov-11 14:47pm
   
Correct, a 5. I credited your solution in my generalized solution, please see.
--SA
LanFanNinja at 14-Nov-11 23:23pm
   
Thank you!
Rate this: bad
good
Please Sign up or sign in to vote.

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());
          }
          Console.ReadLine();
  Permalink  
Comments
LanFanNinja at 14-Nov-11 23:24pm
   
+5 Nice! I did not think to use a lamda.
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 4

  Permalink  

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

  Print Answers RSS
0 OriginalGriff 400
1 Jochen Arndt 200
2 Richard MacCutchan 135
3 DamithSL 95
4 Maciej Los 95
0 OriginalGriff 6,045
1 DamithSL 4,601
2 Maciej Los 4,032
3 Kornfeld Eliyahu Peter 3,480
4 Sergey Alexandrovich Kryukov 3,220


Advertise | Privacy | Mobile
Web04 | 2.8.141220.1 | Last Updated 14 Nov 2011
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100