Click here to Skip to main content
15,891,629 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hi:


Make a dictionary data structure by reading in a string from keyboard, but one word at a time.
And store words in Array of structures. Each structure contains a word and its count. And then
sort the array in alphabetical order.

Print out the entries in your dictionary in alphabetical order, watching for duplicates. Do
not display a word more than once. Instead, display a count of how many times each word
appeared.

PLEASE DO NOT USE ArrayList and List<T> in the collections library.


Please Can anybody explain to me this Concept


Thankq
Posted
Comments
Sandeep Mewara 1-Jun-12 2:40am    
Any effort?

This is homework.
Try something on your own and then post questions based on what you have tried.

Check out online documentation on an array on msdn[^].
 
Share this answer
 
Comments
Maciej Los 1-Jun-12 2:56am    
Good answer, my 5!
 
Share this answer
 
Comments
Maciej Los 1-Jun-12 2:56am    
Good links, my 5!
Prasad_Kulkarni 1-Jun-12 6:58am    
My 5!
please see this link i hope it will help

Arrays in C#[^]
 
Share this answer
 
C#
struct WordInfo
{
    public string Word { get; set; }
    public int count { get; set; }
}
class WordCountDictionary
{
    public static void Main()
    {
        Dictionary<int,> dict = new Dictionary<int,>();
        string EnteredWord;
        int j = 0;
        Boolean flag = true;
        WordInfo wordInfo = new WordInfo();
        while ((EnteredWord = Console.ReadLine()) != "0")
        {
            string[] wordss = EnteredWord.Split(' ');
            foreach (string words in wordss)
            {
                wordInfo.Word = words;
                foreach (var h in dict.ToArray())
                {
                    if (wordInfo.Word.Equals(h.Value.Word))
                    {
                        flag = false;
                        WordInfo word = dict[h.Key];
                        word.count++;
                        dict[h.Key] = word;
                    }
                }
                if (flag)
                {
                    wordInfo.count = 1;
                    dict.Add(j, wordInfo);
                    j++;
                }
                flag = true;
            }
        }
        var sortedOrder = dict.OrderBy(o => o.Value.Word);

        foreach (var i in sortedOrder)
        {
            Console.WriteLine("Key:{0}  Word {1} Count {2}", i.Key, i.Value.Word, i.Value.count);
        }
        Console.ReadLine();
    }
}
 
Share this answer
 
v2
C#
class Program
{
    static void Main(string[] args)
    {
        Console.WriteLine("Press 0 to exit");

        Dictionary<int, Structure> dict = new Dictionary<int, Structure>();

        string readLine;

        while ("0" != (readLine = Console.ReadLine()))
        {
            string line = readLine;
            KeyValuePair<int, Structure> firstOrDefault = dict.FirstOrDefault(ob => ob.Value.Word == line);

            Structure val = new Structure { Count = firstOrDefault.Value.Count + 1, Word = readLine };
            int dictionaryCount = dict.Count;

            if (!firstOrDefault.Equals(default(KeyValuePair<int, Structure>)))
            {
                dict.Remove(firstOrDefault.Key);
                dictionaryCount = firstOrDefault.Key;
            }
            dict.Add(dictionaryCount, val);
        }

        Structure[] structures = dict.Values.ToArray();
        Array.Sort(structures);

        foreach (var structure in structures)
        {
            Console.WriteLine("Word : {0} -> Count : {1}",structure.Word ,structure.Count);
        }
        Console.ReadKey();
    }

    struct Structure : IComparable
    {
        public string Word { get; set; }
        public int Count { get; set; }

        public int CompareTo(object obj)
        {
            return Word.CompareTo(((Structure)obj).Word);
        }
    }
}
 
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