Advertisement
IvayloGeorgiev

Fake Text Markup Language v2

Jan 20th, 2014
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.86 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text.RegularExpressions;
  5.  
  6. namespace Tests
  7. {
  8.     class Program
  9.     {
  10.         static void Main(string[] args)
  11.         {
  12.             int n = int.Parse(Console.ReadLine());
  13.             string pattern = @"<(?<tag>.*?)>(?<text>[^<]*?)</\k<tag>>";            
  14.             string lines = string.Empty;
  15.  
  16.             for (int count = 0; count < n; count++) lines += "\n" + Console.ReadLine();
  17.  
  18.             Match match = Regex.Match(lines, pattern);
  19.             while (match.Success)
  20.             {
  21.                 lines = lines.Replace(match.Value, Change(match.Groups["tag"].Value, match.Groups["text"].Value));
  22.                 match = Regex.Match(lines, pattern);
  23.             }
  24.             Console.WriteLine(lines);
  25.         }
  26.  
  27.         static string Change(string action, string text)
  28.         {
  29.             switch (action)
  30.             {
  31.                 case "upper":
  32.                     {
  33.                         return text.ToUpper();
  34.                     }
  35.                 case "lower":
  36.                     {
  37.                         return text.ToLower();
  38.                     }
  39.                 case "toggle":
  40.                     {
  41.                         char[] result = text.ToCharArray();
  42.                         for (int i = 0; i < result.Length; i++)
  43.                         {
  44.                             if (char.IsLower(result[i])) result[i] = char.ToUpper(result[i]);
  45.                             else if (char.IsUpper(result[i])) result[i] = char.ToLower(result[i]);
  46.                         }
  47.                         return (new string(result));
  48.                     }
  49.                 case "rev":
  50.                     {
  51.                         return new string(text.Reverse().ToArray());
  52.                     }
  53.             }
  54.             return string.Empty;
  55.         }
  56.     }
  57. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement