1. Engineering
  2. Computer Science
  3. this is done in c if you were to run...

Question: this is done in c if you were to run...

Question details

This is done in C++. If you were to run the code in it's current state, an example input and output would look like this: Input: 1 2 3 4 5 Output: [{1}, {2}, {3}, {4}, {5}]. How would you modify the code to allow multiple characters into one curly brackets using quotation marks. For instance, Input 1 2 "3 4 5" would result in [{1}, {2}, {3 4 5}]. Another example, Input: The fish "is swimming" in "the water" would result in [{The}, {fish}. {is swimming}, {in}, {the water}]. Any help would be appreciated.

#include <stdio.h>
#include <cstring>
#include <iostream>
#include <string>
#include <ctype.h>
#include <malloc.h>
#include <readline/readline.h>
#include <readline/history.h>

using namespace std;

struct Node{
char* data;
Node *next;
};

struct Node *head = NULL;
int length = 0;

void addNode(char* data){
Node* newnode = (struct Node*) malloc(sizeof(struct Node));
newnode->data = data;
newnode->next = NULL;
Node* temp = head;

if(head != NULL){
while(temp->next != NULL)
temp = temp -> next;
temp-> next = newnode;
}
else
head = newnode;
length++; }

void print(){
Node* temp = head;
while(temp != NULL){
cout << "{" << temp->data << "}";
temp = temp->next;
}
}

int main(){
static char* line_input = readline("> ");
char* tok = strtok(line_input, " ");
while(tok != NULL){
addNode(tok);
tok = strtok(NULL, " ");
}
cout << "[";
print();
cout << "]";
}

Solution by an expert tutor
Blurred Solution
This question has been solved
Subscribe to see this solution