Advertisement
sylviapsh

Translate Word With Dictionary

Jan 30th, 2013
178
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.51 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3.  
  4. class TranslateWordWithDictionary
  5. {
  6.   //A dictionary is stored as a sequence of text lines containing words and their explanations. Write a program that enters a word and translates it by using the dictionary. Sample dictionary:
  7.   //.NET – platform for applications from Microsoft
  8.   //CLR – managed execution environment for .NET
  9.   //namespace – hierarchical organization of classes
  10.  
  11.   static void Main()
  12.   {
  13.     var sampleDictionary = new Dictionary<string, string >(StringComparer.OrdinalIgnoreCase){
  14.                                                                                               { ".NET", "– platform for applications from Microsoft"},
  15.                                                                                               {"CLR", "– managed execution environment for .NET"},
  16.                                                                                               {"namespace", "– hierarchical organization of classes" }
  17.                                        
  18.                                                                                             };
  19.     Console.Write("Find a definition for: ");
  20.     string wordTosearch = Console.ReadLine();
  21.  
  22.     try
  23.     {
  24.       Console.WriteLine("{0} {1}", wordTosearch, sampleDictionary[wordTosearch.ToLowerInvariant()]);
  25.     }
  26.       catch (KeyNotFoundException)
  27.     {
  28.  
  29.       Console.WriteLine("The word '{0}' cannot be found in the dictionary!", wordTosearch);
  30.     }
  31.    
  32.   }
  33. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement