Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //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: We are living in a <upcase>yellow submarine</upcase>. We don't have <upcase>anything</upcase> else.
- //The expected result: We are living in a YELLOW SUBMARINE. We don't have ANYTHING else.
- using System;
- using System.Collections.Generic;
- using System.Text;
- class TextInTagsToUpper
- {
- static string ChangeTextInTagsToUpper(string str)
- {
- string openTag = "<upcase>";
- string closeTag = "</upcase>";
- int startIndex = str.IndexOf(openTag, 0);
- while (startIndex != -1)
- {
- int endIndex = str.IndexOf(closeTag, startIndex + openTag.Length);
- string textInTagsAndTags = str.Substring(startIndex, endIndex - startIndex + closeTag.Length);
- string textInTags = str.Substring(startIndex + openTag.Length, endIndex - (startIndex + openTag.Length));
- str = str.Replace(textInTagsAndTags, textInTags.ToUpper());
- startIndex = str.IndexOf(openTag, startIndex + 1);
- }
- return str;
- }
- static void Main()
- {
- string input = "We are living in a <upcase>yellow submarine</upcase>. We don't have <upcase>anything</upcase> else.";
- string result = ChangeTextInTagsToUpper(input);
- Console.WriteLine("Result: " + result);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement