Advertisement
sylviapsh

Make Uppercase the Text Surrounded By Tags

Jan 29th, 2013
222
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.91 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3.  
  4. class MakeUppercaseTextSurroundedByTags
  5. {
  6.   //You are given a text. Write a program that changes the text in all regions surrounded by the tags <upcase> and </upcase> to uppercase. The tags cannot be nested. Example:
  7.  
  8.   //We are living in a <upcase>yellow submarine</upcase>. We don't have <upcase>anything</upcase> else.
  9.   //The expected result:
  10.   //We are living in a YELLOW SUBMARINE. We don't have ANYTHING else.
  11.  
  12.   static List<int> FindInTextIndices(string text, string substrToSearch)//Returns a list with the indices of the searchhed substring in a text
  13.   {
  14.     List<int> tagsPositions = new List<int>();
  15.     int lastSubstrFoundIndex = text.LastIndexOf(substrToSearch);
  16.  
  17.     for (int index = 0; index < lastSubstrFoundIndex; )
  18.     {
  19.       index = text.ToLowerInvariant().IndexOf(substrToSearch, index);
  20.       tagsPositions.Add(index);
  21.       index += substrToSearch.Length;
  22.     }
  23.  
  24.     return tagsPositions;
  25.   }
  26.  
  27.   static string MakeUppercaseSubstring(string text, List<int> startIndices, List<int> endIndices)//Makes the text between the tags Uppercase
  28.   {
  29.     for (int i = 0; i < startIndices.Count; i++)
  30.             {
  31.         string substring = text.Substring(startIndices[i], endIndices[i] - startIndices[i] + 1).ToUpper();
  32.         text = text.Replace((text.Substring(startIndices[i], endIndices[i] - startIndices[i] + 1)), substring);
  33.             }
  34.     return text;
  35.   }
  36.  
  37.   static string RemoveTags(string text, string tag, List <int> tagIndices)//Removes the tags from the text
  38.   {
  39.     for (int index = tagIndices.Count-1; index >=0; index--)
  40.     {
  41.       text = text.Remove(tagIndices[index], tag.Length);
  42.     }
  43.  
  44.     return text;
  45.   }
  46.  
  47.   static void Main()
  48.   {
  49.     string text = "We are living in a <upcase>yellow submarine</upcase>. We don't have <upcase>anything</upcase> else.";
  50.  
  51.     string openingTag = "<upcase>",
  52.            closingTag = "</upcase>";
  53.     List<int> openingTagIndices = FindInTextIndices(text,openingTag);//Find the indices of the opening tag
  54.     List<int> closingTagIndices = FindInTextIndices(text, closingTag);//Find the indices of the closing tag
  55.  
  56.     if (openingTagIndices == null || closingTagIndices == null)//If one of the arrays is null
  57.     {
  58.       Console.WriteLine("At least one of the tags haven't been found in the text!");
  59.     }
  60.     else
  61.     {
  62.       string result = MakeUppercaseSubstring(text, openingTagIndices, closingTagIndices);//Make uppercase the text between the tags
  63.       result = RemoveTags(result, openingTag, openingTagIndices);//Remove the opening tags
  64.  
  65.       closingTagIndices.Clear();//Clear the array with the end tag indices because they have changed
  66.       closingTagIndices = FindInTextIndices(result, closingTag);//Find the new indices of the closing tags
  67.       result = RemoveTags(result, closingTag, closingTagIndices);//Remove the closing tags
  68.  
  69.       Console.WriteLine(result);//Print the result
  70.     }
  71.   }
  72. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement