#include <iostream>
#include <string>
using namespace std;
//Turn off loops - you should not need them!
#define for "OhNoYouDont"
#define while "GetThatOutOfMyKitchen"
//Do not modify anything on or above the line below this
//YOUR_CODE_BELOW
/*************************************************************/
/* */
/* CAUTION! ATTENTION! POZOR! 小心!危険! ОСТОРОЖНО! ACHTUNG! */
/* */
/* This implementation deviates from your teacher's */
/* specification in the following ways */
/* */
/* 1) Your teacher's specification allows a TAG to contain ] */
/* Example: */
/* Input: [body]limb]text1[/body]text2[/body]limb] */
/* Output: body:limb]text1 */
/* Output: body]limb:text1[/body]text2 */
/* Both outputs satisfy the specification. */
/* However, to allow a TAG to contain ] would call for */
/* an implementation containing iteration. */
/* That could be done using recursion, which would */
/* not violate the letter, but would violate the spirit */
/* of your teacher's prohibition of loops. */
/* Therefore my implementation does not allow a TAG */
/* to contain ']'. */
/* */
/* 2) This implementation does satisfy your teacher's */
/* specification in allowing text before the initial TAG */
/* and after the final TAG. */
/* However, the specification saying */
/* "print out the text between the first ] and next [" */
/* implies that the first ] and next [ may not be */
/* involved with the TAG. In contrast, this */
/* implementation prints the text between the TAGs. */
/* Example: */
/* Input: ][body]text[[/body] */
/* Teacher's specification: */
/* Output: body:[body]text */
/* This implementation: */
/* Output: body:text[ */
/*************************************************************/
/* This function provides a uniform printing of error messages
into standard error.
It returns a non-zero return code.
*/
int fail(string msg)
{
cerr << endl;
cerr << "** ERROR ** " << msg << endl;
cerr << "The required input format is " << endl;
cerr << "[TAG]text inside tag[/TAG], where TAG is any string not containing ']'" << endl;
return 1;
}
int main()
{
string line; // User input
cout << "Input: "; // Request user input
getline(cin, line);
/* Find the initial tag, which this implementation
(but not your teacher's specification)
requires to start at the first [
*/
size_t startBracePos = line.find('['); // Search line for the first [
if (startBracePos == string::npos) return fail("There is no TAG.");
/* The initial tag extends to the first ] after the first [.
*/
size_t endBracePos = line.find(']', startBracePos);
if (endBracePos == string::npos) return fail("The initial TAG does not have a terminating ']'.");
/* Form the finalTag that should terminate the text
*/
string finalTag = ("[/" + // additional /
line.substr(startBracePos+1, // TAG starts after [
endBracePos-startBracePos)); // length includes ]
/* Find first occurence of final TAG
*/
size_t finalTagPos = line.find(finalTag, endBracePos);
if (finalTagPos == string::npos) return fail("There is no final tag '" + finalTag + "'.");
/* Success!
*/
cout << "Output: "
<< line.substr(startBracePos+1, // TAG starts after [
endBracePos-startBracePos-1) // length does not include ]
<< ":"
<< line.substr(endBracePos+1, // text starts after ]
finalTagPos-endBracePos-1) // till (not incl.) [
<< endl;
return 0; // 0 return code indicates no error
}