Advertisement
shristoff

7.StringAndTextProcessing-14

Jan 28th, 2013
150
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.63 KB | None | 0 0
  1. /* A dictionary is stored as a sequence of text lines containing words and their explanations.
  2.  Write a program that enters a word and translates it by using the dictionary. */
  3.  
  4. using System;
  5. using System.Text.RegularExpressions;
  6.  
  7. class TranslateWordWithDict
  8. {
  9.     static void Main()
  10.     {
  11.         string dictionary =
  12. @".NET – platform for applications from Microsoft
  13. CLR – managed execution environment for .NET
  14. namespace – hierarchical organization of classes
  15. method – a code block containing a series of statements.";
  16.         Match word = Regex.Match(dictionary, @"^[^\s]+", RegexOptions.Multiline);  //regex for the word with Multiline option
  17.         Match definition = Regex.Match(dictionary, @"[–\s].\b[\w]*.+", RegexOptions.Multiline); //regex for the definition with Multiline option
  18.         Console.Write("Please enter term (.NET, CLR, namespace or method): ");
  19.         string wordToExplain = Console.ReadLine();  //reading of the word to know which definition we should provide
  20.         while (word.Success)
  21.         {
  22.             if (wordToExplain == word.Value)    //if we recognize the wntered word
  23.             {
  24.                 Console.WriteLine("{0} {1}", word.Value, definition.Value);  //user-friendly print of the word and the definition
  25.                 break;                                                      //finish the program
  26.             }
  27.             word = word.NextMatch();                    //if no word is recognized go to the next word to check for it
  28.             definition = definition.NextMatch();        //move to the next definition also
  29.         }
  30.         Console.WriteLine();
  31.     }
  32. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement