Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Text;
- using System.Text.RegularExpressions;
- class Program
- {
- static void Main()
- {
- StringBuilder text = new StringBuilder();
- int count = int.Parse(Console.ReadLine());
- string code = null;
- while (count > 0)
- {
- code += Console.ReadLine() ;
- code += "\r\n";
- count--;
- }
- // regular expressions that match the tags but not nested tags
- string upper = @"(<upper>([a-zA-Z0-9\s!?""]+)</upper>)";
- string lower = @"(<lower>([a-zA-Z0-9\s!?""]+)</lower>)";
- string rev = @"(<rev>([a-zA-Z0-9\s!?""]+)</rev>)";
- string toggle = @"(<toggle>([a-zA-Z0-9\s!?""]+)</toggle>)";
- // the cycle continues while there is a match
- while (Regex.IsMatch(code, toggle) || Regex.IsMatch(code, rev)|| Regex.IsMatch(code, lower)||Regex.IsMatch(code, upper))
- {
- code = Regex.Replace(code, upper, m => m.Groups[2].Value.ToUpper()); // sets upper case and deletes tags
- code = Regex.Replace(code, lower, m => m.Groups[2].Value.ToLower()); // sets lower and del tags
- if (Regex.IsMatch(code, toggle)) //match toggle
- {
- Match strTog = Regex.Match(code, toggle); // takes the match
- string[] split = strTog.ToString().Split(new char[] { '>', '<' }, StringSplitOptions.RemoveEmptyEntries); // splits the match
- char[] letters = split[1].ToCharArray(); //splits element[1] from the string because it holds the text after the first split
- string finalTog = null;
- foreach (var ch in letters)
- {
- finalTog += (char.IsLower(ch) ? char.ToUpper(ch) : char.ToLower(ch)); // toggles the chars in the text
- }
- Regex regx = new Regex(toggle);
- code = regx.Replace(code,finalTog,1); // replaces the old text with toggled text and deletes the tags
- }
- if (Regex.IsMatch(code, rev)) //match reverse
- {
- Match strRev = Regex.Match(code, rev, RegexOptions.Singleline); // takes the match
- string[] local = strRev.ToString().Split(new char[] { '>', '<' }, StringSplitOptions.RemoveEmptyEntries); // splits the match
- char[] localLetters = local[1].ToCharArray(); //splits element[1] from the string because it holds the text after the first split
- Array.Reverse(localLetters); //reverse array
- string finalRev = null;
- foreach (var ch in localLetters)
- {
- finalRev += ch; //build new string with reversed letters
- }
- Regex rgx = new Regex(rev);
- code = rgx.Replace(code, finalRev, 1); // replase the old text with reversed and delete the tags
- }
- }
- Console.WriteLine(code);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement