Advertisement
IPetrov007

WordEncounter_100

Apr 11th, 2017
379
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.74 KB | None | 0 0
  1. namespace Word_encounter
  2. {
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Linq;
  6. using System.Text.RegularExpressions;
  7.  
  8. public class WordEncounter
  9. {
  10. public static void Main()
  11. {
  12. var filter = Console.ReadLine();
  13. var letter = filter[0];
  14. var neededCount = int.Parse(filter[1].ToString());
  15. var filteredWords = new List<string>();
  16.  
  17. var sentence = Console.ReadLine();
  18.  
  19. while (sentence != "end")
  20. {
  21. string patternValidSentence = @"^[A-Z].*[.!?]$";
  22. var validSentence = new Regex(patternValidSentence);
  23.  
  24. if (validSentence.IsMatch(sentence))
  25. {
  26. var regex = new Regex(@"\w+");
  27.  
  28. var words = regex.Matches(sentence);
  29.  
  30. foreach (Match item in words)
  31. {
  32. var word = item.Groups[0].Value;
  33.  
  34. var count = 0;
  35. foreach (var character in word)
  36. {
  37. if (character== letter)
  38. {
  39. count++;
  40. }
  41. }
  42.  
  43. if (count >= neededCount)
  44. {
  45. filteredWords.Add(word);
  46. }
  47. }
  48. }
  49.  
  50. sentence = Console.ReadLine();
  51. }
  52.  
  53. Console.WriteLine(string.Join(", ", filteredWords));
  54. }
  55. }
  56. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement