Advertisement
dobroslav_atanasov

WordEncounter

Apr 9th, 2017
223
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.18 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text.RegularExpressions;
  5.  
  6. namespace _03.WordEncounter
  7. {
  8.     public class WordEncounter
  9.     {
  10.         public static void Main()
  11.         {
  12.             var words = new List<string>();
  13.  
  14.             var filter = Console.ReadLine();
  15.             var symbol = filter[0];
  16.             var digitString = string.Empty;
  17.  
  18.             for (int i = 1; i < filter.Length; i++)
  19.             {
  20.                 digitString += filter[i];
  21.             }
  22.  
  23.             var number = int.Parse(digitString);
  24.  
  25.             var listOfSentences = Console.ReadLine();
  26.  
  27.             var pattern = @"[A-Z](.*?)(\.|\?|\!)";
  28.             var regex = new Regex(pattern);
  29.  
  30.             while (true)
  31.             {
  32.                 if (listOfSentences == "end")
  33.                 {
  34.                     break;
  35.                 }
  36.  
  37.                 var isValidSentence = regex.IsMatch(listOfSentences);
  38.  
  39.                 if (isValidSentence)
  40.                 {
  41.                     var matches = regex.Matches(listOfSentences);
  42.  
  43.                     foreach (Match match in matches)
  44.                     {
  45.                         var sentenceToString = match.Value;
  46.                         var wordsInSentence = sentenceToString.Split(new char[] { ' ', ',', '.', '!', '?', ';', ':', '(', ')', '-' }, StringSplitOptions.RemoveEmptyEntries);
  47.  
  48.                         foreach (var word in wordsInSentence)
  49.                         {
  50.                             var count = 0;
  51.                             for (int i = 0; i < word.Length; i++)
  52.                             {
  53.                                 if (word[i] == symbol)
  54.                                 {
  55.                                     count++;
  56.                                 }
  57.                             }
  58.  
  59.                             if (count == number)
  60.                             {
  61.                                 words.Add(word);
  62.                             }
  63.                         }
  64.                     }
  65.                 }
  66.  
  67.                 listOfSentences = Console.ReadLine();
  68.             }
  69.  
  70.             Console.WriteLine(string.Join(", ", words));
  71.         }
  72.     }
  73. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement