Advertisement
nina75

Strings/Task5

Jan 18th, 2014
54
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.43 KB | None | 0 0
  1. //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.
  2. //Example: We are living in a <upcase>yellow submarine</upcase>. We don't have <upcase>anything</upcase> else.
  3. //The expected result: We are living in a YELLOW SUBMARINE. We don't have ANYTHING else.
  4.  
  5. using System;
  6. using System.Collections.Generic;
  7. using System.Text;
  8. class TextInTagsToUpper
  9. {
  10.     static string ChangeTextInTagsToUpper(string str)
  11.     {
  12.         string openTag = "<upcase>";
  13.         string closeTag = "</upcase>";
  14.         int startIndex = str.IndexOf(openTag, 0);
  15.  
  16.         while (startIndex != -1)
  17.         {
  18.             int endIndex = str.IndexOf(closeTag, startIndex + openTag.Length);
  19.             string textInTagsAndTags = str.Substring(startIndex, endIndex - startIndex + closeTag.Length);
  20.             string textInTags = str.Substring(startIndex + openTag.Length, endIndex - (startIndex + openTag.Length));
  21.  
  22.             str = str.Replace(textInTagsAndTags, textInTags.ToUpper());
  23.             startIndex = str.IndexOf(openTag, startIndex + 1);
  24.         }
  25.  
  26.         return str;
  27.     }
  28.  
  29.     static void Main()
  30.     {
  31.         string input = "We are living in a <upcase>yellow submarine</upcase>. We don't have <upcase>anything</upcase> else.";
  32.         string result = ChangeTextInTagsToUpper(input);
  33.         Console.WriteLine("Result: " + result);
  34.     }
  35. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement