optybg

Untitled

Apr 30th, 2017
193
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.96 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6.  
  7. namespace Worm_Ipsum
  8. {
  9.     class WormIpsum
  10.     {
  11.         static void Main()
  12.         {
  13.  
  14.  
  15.             var input = Console.ReadLine();
  16.  
  17.             while (input != "Worm Ipsum")
  18.             {
  19.                 var sentence = input.Split(new[] { '.' }, StringSplitOptions.RemoveEmptyEntries);
  20.  
  21.                 var isCapitalChar = Char.IsUpper(sentence[0][0]);
  22.  
  23.                 if (!(sentence.Length == 1 && isCapitalChar))
  24.                 {
  25.                     input = Console.ReadLine();
  26.                     continue;
  27.                 }
  28.                 var sent = sentence[0];
  29.                 var shit1 = sent.IndexOf('\'');
  30.                 var shit2 = sent.IndexOf(',');
  31.  
  32.                 var wordsInSentence = sentence[0].Split(new[] { ' ', '\'', ',' }, StringSplitOptions.RemoveEmptyEntries).ToList();
  33.  
  34.                 // foreach (var word in wordsInSentence)
  35.                 for (int i = 0; i < wordsInSentence.Count; i++)
  36.                 {
  37.                     var word = wordsInSentence[i];
  38.                     Dictionary<char, int> dict = new Dictionary<char, int>();
  39.  
  40.                     int max = 0;
  41.                     foreach (char c in word)
  42.                     {
  43.                         int j;
  44.                         dict.TryGetValue(c, out j);
  45.                         j++;
  46.                         if (j > max)
  47.                         {
  48.                             max = j;
  49.                         }
  50.                         dict[c] = j;
  51.                     }
  52.  
  53.                     var mostOccurrenceChar = dict.OrderByDescending(x => x.Value);
  54.  
  55.                     var theChar = mostOccurrenceChar.First().Key.ToString().ToCharArray();
  56.  
  57.                     //var theChar = mostOccurrenceChar.Key().First().ToString().ToCharArray();
  58.  
  59.                     var theCount = int.Parse(mostOccurrenceChar.First().Value.ToString());
  60.  
  61.                     if (theCount >= 2)
  62.                     {
  63.                         var newWord = new string(theChar[0], word.Length);
  64.                         var index = wordsInSentence.IndexOf(word);
  65.                         wordsInSentence[index] = newWord;
  66.                     }
  67.  
  68.                 }
  69.                 var result = string.Join(" ", wordsInSentence) + ".";
  70.  
  71.                 if (shit1 != -1)
  72.                 {
  73.                     StringBuilder sb = new StringBuilder(result);
  74.                     sb[shit1] = '\'';
  75.                     result = sb.ToString();
  76.  
  77.                 }
  78.  
  79.                 if (shit2 != -1)
  80.                 {
  81.                     StringBuilder sb = new StringBuilder(result);
  82.                     sb[shit2] = ',';
  83.                     result = sb.ToString();
  84.                     result = result.Replace(",", ", ");
  85.  
  86.                 }
  87.  
  88.                 Console.WriteLine(result);
  89.  
  90.  
  91.                 input = Console.ReadLine();
  92.             }
  93.  
  94.         }
  95.     }
  96. }
Advertisement
Add Comment
Please, Sign In to add comment