Advertisement
zdravko7

[C# Advanced Exam] 03. Text Transformer

Jun 2nd, 2015
325
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.42 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.  
  7. class TextTransformer
  8. {
  9.     static void Main(string[] args)
  10.     {
  11.         StringBuilder input = new StringBuilder();
  12.  
  13.         while (true)
  14.         {
  15.             string inputString = Console.ReadLine();
  16.             if (inputString == "burp") break;
  17.             input.Append(inputString);
  18.         }
  19.  
  20.         string finalString = String.Concat(input);
  21.  
  22.         finalString = System.Text.RegularExpressions.Regex.Replace(finalString, @"\s+", " ");
  23.  
  24.         string pattern = @"(\$([^'%&$]+)\$)|(\&([^'%&$]+)\&)|(%([^'%&$]+)%)|(\'([^'%&$]+)\')";
  25.         MatchCollection matches = Regex.Matches(finalString, pattern, RegexOptions.IgnorePatternWhitespace);
  26.  
  27.         //checks for dollars
  28.         foreach (var match in matches)
  29.         {
  30.             string currentMatch = match.ToString();
  31.  
  32.             string result = "";
  33.             for (int i = 0; i < currentMatch.Length; i++)
  34.             {
  35.                 switch (currentMatch[0])
  36.                 {
  37.                     case '$':
  38.  
  39.                         if (i % 2 == 0)
  40.                         {
  41.                             result += (char)(currentMatch[i] - 1);
  42.                             result = result.Trim(new char[] { ((char)(currentMatch[0] - 1)) });
  43.                         }
  44.                         else
  45.                         {
  46.                             result += (char)(currentMatch[i] + 1);
  47.                             result = result.Trim(new char[] { ((char)(currentMatch[0] + 1)) });
  48.                         }
  49.                         break;
  50.  
  51.                     case '%':
  52.  
  53.                         if (i % 2 == 0)
  54.                         {
  55.                             result += (char)(currentMatch[i] - 2);
  56.                             result = result.Trim(new char[] { ((char)(currentMatch[0] - 2)) });
  57.                         }
  58.                         else
  59.                         {
  60.                             result += (char)(currentMatch[i] + 2);
  61.                             result = result.Trim(new char[] { ((char)(currentMatch[0] + 2)) });
  62.                         }
  63.                         break;
  64.  
  65.                     case '&':
  66.  
  67.                         if (i % 2 == 0)
  68.                         {
  69.                             result += (char)(currentMatch[i] - 3);
  70.                             result = result.Trim(new char[] { ((char)(currentMatch[0] - 3)) });
  71.                         }
  72.                         else
  73.                         {
  74.                             result += (char)(currentMatch[i] + 3);
  75.                             result = result.Trim(new char[] { ((char)(currentMatch[0] + 3)) });
  76.                         }
  77.                         break;
  78.  
  79.                     case '\'':
  80.  
  81.                         if (i % 2 == 0)
  82.                         {
  83.                             result += (char)(currentMatch[i] - 4);
  84.                             result = result.Trim(new char[] { ((char)(currentMatch[0] - 4)) });
  85.                         }
  86.                         else
  87.                         {
  88.                             result += (char)(currentMatch[i] + 4);
  89.                             result = result.Trim(new char[] { ((char)(currentMatch[0] + 4)) });
  90.                         }
  91.                         break;
  92.                 }
  93.             }
  94.             Console.Write(result + " ");
  95.         }
  96.     }
  97. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement