Advertisement
sivancheva

WormIpsum

Oct 30th, 2017
199
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.49 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Text.RegularExpressions;
  6. using System.Threading.Tasks;
  7.  
  8. namespace _02_WormIpsum
  9. {
  10. class WormIpsum
  11. {
  12. static void Main(string[] args)
  13. {
  14. var input = Console.ReadLine();
  15.  
  16. var pattern = (@"^([A-Z]{1})([^\.]*)\.$");
  17.  
  18. while (input != "Worm Ipsum")
  19. {
  20.  
  21. if (!Regex.IsMatch(input, pattern))
  22. {
  23. input = Console.ReadLine();
  24. continue;
  25. }
  26. var inputArr = input.Split(new[] { ' ','.' }, StringSplitOptions.RemoveEmptyEntries).Select(x => x.Trim()).ToArray();
  27.  
  28. foreach (var word in inputArr)
  29. {
  30. var mostRepeatedLetter = word.GroupBy(x => x).OrderByDescending(x => x.Count()).First().Key;
  31. int ocurrences = word.Count(c => c == mostRepeatedLetter);
  32. if (ocurrences >= 2)
  33. {
  34. string wordToReplace = new string(mostRepeatedLetter, word.Length);
  35. var index = Array.FindIndex(inputArr, x => x == word);
  36. inputArr[index] = wordToReplace;
  37. }
  38. }
  39. Console.WriteLine(String.Join(" ",inputArr)+ ".");
  40. input = Console.ReadLine();
  41. }
  42. }
  43. }
  44. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement