[C# Question of the Day] Count the number of words and find the word with the highest frequency

Hits: 0

Assignment 1: Count the number of words in the txt text, and find out which word has the highest frequency?

Running result:
the above code:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            //File open 
            //string file = System.IO.File.ReadAllLines(@""); 
            int count = 0 ;
             string tmp = "" ;
             //initialization times 
            string words = "hihi hello,hihi,hello,hihi? " ;
             var new_i = words.Split( new  char [] { ' ' , ',' , '.' , '?' },StringSplitOptions.RemoveEmptyEntries);
            Console.Write( "Total number of words: {0}\n" , new_i.Length);
             for ( int i = 0 ; i < new_i.Length; i++)
            {
                //Query the number of occurrences of each word 
                var query = from key in new_i where key.ToUpperInvariant() == new_i[i].ToUpperInvariant() select key;
                 int key_count = query.Count();
                 if (key_count > count) {

                    count = key_count;
                    tmp = new_i[i];
                }

            }

            Console.Write( "The word with the highest frequency is: {0}\n" , tmp);
            Console.Write( "Number of times: {0}\n" , count);

            Console.ReadKey();

        }
    }
}

========================

The base code runs successfully! Open through the outside!

to open externally:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            //File open 
            //string[] file_A = System.IO.File.ReadAllLines(@"C:\Users\Administrator\Desktop.txt"); 
            string file_A = System.IO.File.ReadAllText( @"C :\Users\Administrator\Desktop.txt" );
             //Console.Write(file_A); 
            int count = 0 ;
             string tmp = "" ;
             //initialization times

            var new_i = file_A.Split(new char[] { ' ', ',', '.', '?' }, StringSplitOptions.RemoveEmptyEntries);
            Console.Write( "Total number of words: {0}\n" , new_i.Length);
             for ( int i = 0 ; i < new_i.Length; i++)
            {
                //Query the number of occurrences of each word 
                var query = from key in new_i where key.ToUpperInvariant() == new_i[i].ToUpperInvariant() select key;
                 int key_count = query.Count();
                 if (key_count > count) {

                    count = key_count;
                    tmp = new_i[i];
                }

            }

            Console.Write( "The word with the highest frequency is: {0}\n" , tmp);
            Console.Write( "Number of times: {0}\n" , count);

            Console.ReadKey();

        }
    }
}

Run the screenshot:

You may also like...

Leave a Reply

Your email address will not be published.