| Class name: TCMLParser
Method name: replaceTag
Parameters: String, int, String
Returns: String
You are in charge of a document system that utilizes numeric code tags to
render documents for printing. There is a batch of documents that have text
based tags that you must parse for input into the system. Implement a class
TCMLParser that contains a method replaceTag to convert the text based tags
into a specified numeric code tag.
The method parameters are: String tagstring, int code, String toParse
* tagstring is a String containing letters (a-z and A-Z, inclusive)
representing the TCML tag string to replace. Its length is between 1 and 10,
inclusive.
* code is an int between 1 and 1000, inclusive, representing the code with
which to replace the tag string.
* toParse is a String containing between 1 and 50, inclusive, letters (a-z and
A-Z, inclusive), numbers (0-9, inclusive), less than signs (<), greater than
signs (>), equal signs (=), slashes(/) or spaces. The '<' and '>' may only be
used as part of a tag.
A tag is defined as a '<' that may be followed by letters, numbers, slashes or
spaces and then a '>'. A tag always starts with a '<' and ends with a '>'.
Tags may not be nested.
The following toParse Strings are not valid:
">HI" "<a<b>c>" "<a b c><" "<a<b>"
The following toParse Strings are valid:
"/=<>HI" "/<>H=I<>/" "<><><><>" "<a=/><b==//bb><c223>" "<a b c>"
The method returns a String that is toParse, except every occurrence of
tagstring within a tag should be replaced with code.
Note:
- The string should be processed from the left most character to the right.
- The replace is not case sensitive. If the tagstring is "BODY": "BODY",
"body", and "BoDy" should all be replaced. However, characters not replaced
should be returned in the same case they were inputted.
- Only replace the tagstring if it is between a '<' and before the next '>'.
There can, however, be characters between the '<' and '>' that are not replaced.
- tagstring is not defined by space (' ') boundaries. For example, if
tagstring="b", code=2 and toParse="<b b abc ab c>" then the output would be "<2
2 a2c a2 c>".
The method signature is (be sure your method is public):
String replaceTag(String tagstring, int code, String toParse);
TopCoder will check the following:
* tagstring is a String containing letters (a-z and A-Z, inclusive)
representing the TCML tag string to replace. Its length is between 1 and 10,
inclusive.
* code is an int between 1 and 1000, inclusive, representing the code with
which to replace the tag string.
* toParse is a String containing between 1 and 50, inclusive, letters (a-z and
A-Z, inclusive), numbers (0-9, inclusive), less than signs (<), greater than
signs (>), equal signs (=), slashes(/) or spaces.
* All '<' and '>' are used only in tags
* Tags are not nested
Examples:
If tagstring="BODY", code=10, and toParse="<><BODY garbage>body</BODY>", the
method should return "<><10 garbage>body</10>".
If tagstring="aBc", code=923, and toParse="<dont replace
this>abcabc<abcabcde>", the method should return "<dont replace
this>abcabc<923923de>".
If tagstring="table", code=1, and toParse="<ta>bLe<TaBle
width=100></table></ta>", the method should return "<ta>bLe<1
width=100></1></ta>"
.
If tagstring="replace", code=323, and toParse="nothing inside", the method
should return "nothing inside"
.
|