Home   Cover Cover Cover Cover
 

Statistics

Chap4_Ex4.cs
using System;
using System.Collections;

public class WordFrequency {

  public static void Main(string[] args) {
    char[] sep = {' ',',','.','!','?'};
    Console.Write("text?:");
    string text = Console.ReadLine();
    // split the string into substrings according to the separator array
    string[] s = text.Split(sep); 
    Hashtable hash = new Hashtable();
    for(int i=0;istring sn = s[i].ToLower().Trim();
      if(!sn.Equals("")) { // we do not want to count the spaces 
        if(!hash.ContainsKey(sn)) {
          hash.Add(sn,1);
        }
        else {
          int count = (int)hash[sn] + 1;
          hash.Remove(sn);
          hash.Add(sn,count);
        }
      }
    }
    // print the statistic 
    foreach(DictionaryEntry de in hash) {
      Console.WriteLine("{0}\t: {1}", de.Key, de.Value);
    }

  }


}