Advertisement
dimipan80

Uppercase Words

May 24th, 2015
275
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.02 KB | None | 0 0
  1. /* Write a program to reverse the letters of all uppercase words in a text. In case an uppercase word stays unchanged after reversing its letters, then double each of its letters. A word is a sequence of Latin letters separated by non-letter characters (e.g. punctuation characters or digits).
  2.  * The output should hold the result text. Ensure you escape correctly the HTML special characters in the output with the SecurityElement.Escape() method. */
  3.  
  4. namespace _15.Uppercase_Words
  5. {
  6.     using System;
  7.     using System.Collections.Generic;
  8.     using System.Linq;
  9.     using System.Security;
  10.     using System.Text.RegularExpressions;
  11.  
  12.     class UppercaseWords
  13.     {
  14.         static void Main(string[] args)
  15.         {
  16.             string textLine = Console.ReadLine();
  17.             while (textLine != "END")
  18.             {
  19.                 ReverseAndReplaceUppercaseWords(textLine);
  20.                 textLine = Console.ReadLine();
  21.             }
  22.         }
  23.  
  24.         private static void ReverseAndReplaceUppercaseWords(string text)
  25.         {
  26.             const string pattern = @"(?:^|\W)\b[A-Z]+\d*[A-Z]*\b";
  27.             string resultText = text;
  28.             HashSet<string> matchedWords = new HashSet<string>();
  29.             MatchCollection matches = Regex.Matches(text, pattern);
  30.             foreach (Match match in matches)
  31.             {
  32.                 string word = match.Groups[0].Value;
  33.                 if (matchedWords.Contains(word))
  34.                 {
  35.                     continue;
  36.                 }
  37.  
  38.                 matchedWords.Add(word);
  39.                 Dictionary<string, string> matchedGroups = new Dictionary<string, string>();
  40.                 MatchCollection uppercases = Regex.Matches(word, @"[A-Z]+");
  41.                 foreach (Match uppercase in uppercases)
  42.                 {
  43.                     string upperWord = uppercase.Groups[0].Value;
  44.                     if (matchedGroups.ContainsKey(upperWord))
  45.                     {
  46.                         continue;
  47.                     }
  48.  
  49.                     char[] letters = upperWord.ToCharArray();
  50.                     Array.Reverse(letters);
  51.                     string reversed = new string(letters);
  52.                     if (reversed == upperWord)
  53.                     {
  54.                         char[] doubleReverse = new char[reversed.Length * 2];
  55.                         for (int i = 0; i < reversed.Length; i++)
  56.                         {
  57.                             doubleReverse[2 * i] = reversed[i];
  58.                             doubleReverse[2 * i + 1] = reversed[i];
  59.                         }
  60.  
  61.                         reversed = new string(doubleReverse);
  62.                     }
  63.  
  64.                     matchedGroups.Add(upperWord, reversed);
  65.                 }
  66.  
  67.                 string replacement = matchedGroups
  68.             .Aggregate(word, (current, @group) => current.Replace(@group.Key, @group.Value));
  69.  
  70.                 resultText = resultText.Replace(word, replacement);
  71.             }
  72.  
  73.             Console.WriteLine(SecurityElement.Escape(resultText));
  74.         }
  75.     }
  76. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement