Question: I need help fixing some code written as an answer to a question on here. I have updated the description with input and the corresponding

I need help fixing some code written as an answer to a question on here. I have updated the description with input and the corresponding expected output.

I removed the link because apparently that makes the problem unclear. I am including the rules for determining if something is a token ('cause that's what I provided the link for).

Determining if something is a token:

1. Whitespace (space, tab, etc) ends a token and is skipped unless it is in quotes (see below). You will find the function int isspace(char) useful for detecting whitespace characters. 2. Some characters are considered special tokens: | ; < > &. When one of these characters is encountered, the token being built is completed and a new token consisting of the character is created. 3. The character \ is a special character, i.e. an escape character. The next character will be part (or the start) of a token. 4. Items between single or double quotes are treated as parts of the current token. 5. Characters that do not meet the above rules are added on to the current token being built.

Problems with the current code: The output of we\'ll do some "crazy \"air quotes\"" should not include the first and last ", i.e. crazy "air quotes" is the token and not "crazy "air quotes"". Another example would be an input of I" like Linux", which would give one token of I like Linux as the output (it's recognizing the spaces because the spaces are between quotes).

Correct input/output examples for above:

Input: we\'ll do some "crazy \"air quotes\""

Output: [{we'll},{do},{some},{crazy "air quotes"}]

Input: I" like Linux"

Output: [{I like Linux}]

In cases where it shouldn't, it's treating spaces like tokens. For example,the output of ls | sort >sorted_files.txt should be five tokens, none of which should be spaces.

Correct input/output examples for above:

Input: ls | sort >sorted_files.txt

Output: [{ls},{|},{sort},{>},{sorted_files.txt}]

Please change the code to correct the above issues.

#include #include #include using namespace std; // function to determine if a token vector is_token(const string& s) { vector tokens;

//Get the length of input string

int nLen = s.length(); //variable to hold each token parsed string token; for(int i=0;i { //if the character encountered is space , token construction is completed. if (isspace(s[i])) { //Remove the '' if any size_t spos = token.find("\", 0); if (spos != string::npos) { token.replace(spos, 1, ""); }

//Add the new token to vector tokens.push_back(token); token.clear(); } //if charater is encountered is one of |,;,<,>,& , treat them as 1 token and add it to the tokens vector else if((s[i] == '|') || (s[i] == ';') || (s[i] == '<') || (s[i] == '>') || (s[i] == '&')) { tokens.push_back(token); token.clear(); string temp; temp.push_back(s[i]); tokens.push_back(temp); } else //normal character other than above, continue constructing the token { token.push_back(s[i]); } //if '' is encountered ,add the next character to the current token if (s[i] == '\') { i++; token.push_back(s[i]); } }

//last token, remove '' if any and add the token to vector size_t spos = token.find("\", 0); if (spos != string::npos) { token.replace(spos, 1, ""); }

tokens.push_back(token);

//return the tokens vector return tokens; }

int main() { // loop to get input is accepted until the user hits CTRL+d while (true) { string str; cout << "(Ctrl+D) to exit >"; getline(cin, str);

//if user pressed Ctrl+D exit out of loop if (str.compare("x4") == 0) break;

//vector to store the strings or characters returned by is_token() function vector tokens;

tokens = is_token(str);

cout << " Output tokens: "; for (int i = 0; i < tokens.size(); i++) { cout << tokens[i] << " "; } cout << endl; } }

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!