Advertisement
Danny_Berova

02.WormIpsum

Aug 11th, 2017
128
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.44 KB | None | 0 0
  1.  
  2. namespace _02.WormIpsum
  3. {
  4.     using System;
  5.     using System.Collections.Generic;
  6.     using System.Linq;
  7.     using System.Text.RegularExpressions;
  8.  
  9.     public class Program
  10.     {
  11.         public static void Main()
  12.         {
  13.             var regex = new Regex(@"[A-Z]{1}[\w\W]+?\.{1,}");
  14.             string sentenceLine = Console.ReadLine();
  15.  
  16.             while (sentenceLine != "Worm Ipsum")
  17.             {
  18.                 var matches = regex.Matches(sentenceLine);
  19.  
  20.                 if (matches.Count > 1)
  21.                 {
  22.                     sentenceLine = Console.ReadLine();
  23.                     continue;
  24.                 }
  25.  
  26.                 var sentence = string.Empty;
  27.  
  28.                 var words = sentenceLine.TrimEnd('.').Split(' ');
  29.                 foreach (var word in words)
  30.                 {
  31.                     string currentWord = ProcessWord(word);
  32.  
  33.                     sentence += $" {currentWord}";
  34.                 }
  35.  
  36.                 sentence = sentence.TrimStart();
  37.                 Console.WriteLine($"{sentence}.");
  38.  
  39.                 sentenceLine = Console.ReadLine();
  40.             }
  41.         }
  42.         private static string ProcessWord(string word)
  43.         {
  44.             var elements = new Dictionary<char, int>();
  45.  
  46.             for (int ind = 0; ind < word.Length; ind++)
  47.             {
  48.                 var element = word[ind];
  49.                 if (!elements.ContainsKey(word[ind]))
  50.                 {
  51.                     elements[element] = 0;
  52.                 }
  53.  
  54.                 elements[element]++;
  55.             }
  56.  
  57.             char returned = elements.OrderByDescending(c => c.Value).First().Key;
  58.             var occurences = elements.OrderByDescending(c => c.Value).First().Value;
  59.  
  60.             if (occurences >= 2)
  61.             {
  62.                 string returnedWord = string.Empty;
  63.                 for (int i = 0; i < word.Length; i++)
  64.                 {
  65.                     if (word[i] == ',')
  66.                     {
  67.                         returnedWord += ',';
  68.                     }
  69.                     else if (word[i] == '\'')
  70.                     {
  71.                         returnedWord += '\'';
  72.                     }
  73.                     else
  74.                     {
  75.                         returnedWord += returned;
  76.                     }
  77.                 }
  78.  
  79.                 return returnedWord;
  80.             }
  81.             else
  82.             {
  83.                 return word;
  84.             }
  85.         }
  86.     }
  87. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement