Highlight a Chunk of Text

Most of the common data formats, such as CSV, XML, and JSON, use plain text to store complex data or somehow interpret that plain text as something it’s not. The programmer’s job is to translate the plain text and generate the proper type of complex data.

With formats such as XML and JSON, you can obtain a C library that does all the heavy lifting for you. For other formats, such as CSV, you can write yourself. (Click here for my CSV Exercise from Feb. 2017.)

For this month’s Exercise, I offer a simple type of data format or plain text processing as your challenge. The rule goes like this:

  • The ^ character is a text formatting toggle.
  • When ^ is encountered in a string of text, the following text is translated to UPPER CASE.
  • When the ^ character is encountered again, text returns to lower case.

Here are some sample runs:

Input: This is ^very^ important.
Output: This is VERY important.

Input: You can ^even^ highlight ^multiple^ words.
Output: You can EVEN highlight MULTIPLE words.

Input: And you can forget ^to turn it off.
Output: And you can forget TO TURN IT OFF.

If you use two ^ characters in a row, the format is turned on and off again, so your code doesn’t need a method for generating the ^ character itself as output:

Input: This is weird ^^^^^^output.
Output: This is weird output.

Your task is to write code that accepts a line of input, process that input, and convert text to UPPER CASE as shown in the above examples.

Click here to view my solution, but please try this Exercise on your own before seeing what I did.

Leave a Reply