Question

This program uses C++. This program reads in a line from the user and prints it...

This program uses C++. This program reads in a line from the user and prints it out in a certain format. An example would be Input: 1 2 3 4 5 would result Output: [{1}, {2}, {3}, {4}, {5}]. When quotations marks are added into the input the format becomes different. For instance, Input 1 2 "3 4 5" would result in [{1}, {2}, {3 4 5}]. When I ad multiple quotation marks into the input, it will only use the last portion in quotations. For instance, Input 1 "2 3" 4 "5 6" would result in [{1}, {5 6}, {4}, {5 6}] instead of  [{1}, {2 3}, {4}, {5 6}]. Any help in getting it to print the latter? To compile the program use -lreadline.

#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;
if(temp!=NULL)cout<<",";
}
}

int main(){
static char* line_input = readline("> ");
char* tok = strtok(line_input, " ");
while(tok != NULL){
//cout<<tok<<endl;
char temp[100];
if(tok[0]=='"')
{
strcpy(temp,&tok[1]);
while(tok!=NULL)
{
tok = strtok(NULL, " ");
strcat(temp," ");
  
int l=strlen(tok);
if(tok[l-1]=='"')
{
char d[100];
int k;
for(k=0;k<l-1;k++)
d[k]=tok[k];
d[k]='\0';
strcat(temp,d);
break;
}
else
{
strcat(temp,tok);
}
}
addNode(temp);
}
else
addNode(tok);
tok = strtok(NULL, " ");
}
cout << "[";
print();
cout << "]";
}

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Solution :

#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;
   struct Node *next;
};

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

void print();

int 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++;
return 0;
}

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

int main(){
   static char* line_input = readline("> ");
   char* tok = strtok(line_input, " ");
   int i=1;
   while(tok != NULL){
       cout<<" <<----------["<<i++<<"]--------->>"<<endl;
       char temp[100];
       if(tok[0]=='"')
       {
           strcpy(temp,&tok[1]);
           print();
           printf(" If part..{%s} ", tok);
           while(tok!=NULL)
           {
               print();
               printf("-Inside while ");
               tok = strtok(NULL, " ");
               strcat(temp," ");

               int l=strlen(tok);
               if(tok[l-1]=='"')
               {
                   printf("-Inside while's IFF ");
                   char d[100];
                   int k;
                   for(k=0;k<l-1;k++)
                       d[k]=tok[k];
                   d[k]='';
                   strcat(temp,d);
                   break;
               }
               else
               {
                   printf("-Inside while's ELSE ");
                   strcat(temp,tok);
               }
           }
           print();
           printf(" Calling ADDNODE temp = [%s] ", temp);
           addNode(temp);
       }
       else {
           printf(" Else part..adding node [%s] ", tok);
           addNode(tok);
       }
       tok = strtok(NULL, " ");
   }
   cout << "[";
   print();
   cout << "]"<<endl;
}

Output:

           

From the above output it is clear that the Second nodes data changes while the process in still executing in the strtok. So the problem is with the strtok and remaining functions are working as expected.

Add a comment
Know the answer?
Add Answer to:
This program uses C++. This program reads in a line from the user and prints it...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • #include <iostream> using namespace std; struct ListNode { float value; ListNode *next; }; ...

    #include <iostream> using namespace std; struct ListNode { float value; ListNode *next; }; ListNode *head; class LinkedList { public: int insertNode(float num); void deleteNode(float num); void destroyList(); void displayList(); LinkedList(void) {head = NULL;} ~LinkedList(void) {destroyList();} }; int LinkedList::insertNode(float num) { struct ListNode *newNode, *nodePtr = head, *prevNodePtr = NULL; newNode = new ListNode; if(newNode == NULL) { cout << "Error allocating memory for new list member!\n"; return 1; } newNode->value = num; newNode->next = NULL; if(head==NULL) { cout << "List...

  • 1) Create a struct called CourseInfo to store info about a course (Include variables for courseNum,...

    1) Create a struct called CourseInfo to store info about a course (Include variables for courseNum, courseName, & grade) 2) Change the type for the 'data' member variable in the node struct to CourseInfo (see #1) and rename it to 'courseData' 3) Modify the createNode function to receive a CourseInfo struct as a parameter. It should also display the address of the new node that is created. Display the address in both hex and decimal form. 4) Modify the display...

  • Please rewrite this function using recursive function #include using namespace std; struct Node { char ch;...

    Please rewrite this function using recursive function #include using namespace std; struct Node { char ch; Node* next; }; class LinkedList { Node* head; public: LinkedList(); ~LinkedList(); void add(char ch); bool find(char ch); bool del(char ch); friend std::ostream& operator<<(std::ostream& out, LinkedList& list); }; LinkedList::LinkedList() { head = NULL; } LinkedList::~LinkedList() { Node* cur = head, * tmp; while (cur != NULL) { tmp = cur->next; delete cur; cur = tmp; } } void LinkedList::add(char ch) { Node* cur = head,...

  • Using C, I need help debugging this program. I have a few error messages that I'm...

    Using C, I need help debugging this program. I have a few error messages that I'm not sure how to fix. Here is the code I have: /* * C Program to Print a Linked List in Reverse Order */ #include <stdio.h> #include <stdlib.h> struct node { int num; struct node *next; }; int main() { struct node *p = NULL; struct node_occur *head = NULL; int n; printf("Enter data into the list\n"); create(&p); printf("Displaying the nodes in the list:\n");...

  • Deleting multiples of a given integer from a linked list: #include <stdio.h> #include <stdlib.h> #include <assert.h>...

    Deleting multiples of a given integer from a linked list: #include <stdio.h> #include <stdlib.h> #include <assert.h> #define MAX 10000 typedef struct node_tag { int v; // data struct node_tag * next; // A pointer to this type of struct } node; // Define a type. Easier to use. node * create_node(int v) { node * p = malloc(sizeof(node)); // Allocate memory assert(p != NULL); // you can be nicer // Set the value in the node. p->v = v; p->next...

  • Please fill in this code to reverse a linked list: (written in C/C++) #include #include #include...

    Please fill in this code to reverse a linked list: (written in C/C++) #include #include #include using namespace std; /* Link list node */ struct Node { int data; // your code here }; /* Function to reverse the linked list */ static void reverse(struct Node** head_ref) { // your code here } /* Function to push a node */ void push(struct Node** head_ref, int new_data) { // your code here } /* Function to print linked list */ void...

  • Having code issues wth my C++ program. My program checks if two binary trees are similar...

    Having code issues wth my C++ program. My program checks if two binary trees are similar and if they're not they return false. My program is return true with different binary trees. Could use some help thanks #include <iostream> #include <string> using namespace std; //Struct of Nodes struct BinarySearchTree { int data; BinarySearchTree *left; BinarySearchTree *right; }; // Inserting nodes into BST BinarySearchTree* insert( BinarySearchTree* node, int val) { if (node == NULL) { BinarySearchTree *newNode = new BinarySearchTree(); newNode->data...

  • This is a code for linked list, it is about adding a node in the middle...

    This is a code for linked list, it is about adding a node in the middle of a list, I am really confused why int i = 2? can't it be 0? Add to the Middle • Allocate memory and store data for new node Traverse to node just before the required position of new node Change next pointers to include new node in between struct node *newNode; newNode = malloc(sizeof(struct node)); newNode->data = 4; struct node *temp head; for(int...

  • Hi, I need to make a program in C that reads the type of currency and...

    Hi, I need to make a program in C that reads the type of currency and organizes them into stacks based on currency which can be read back. This is what I have so far, can I get help finishing it? #include <stdio.h> #include <stdlib.h> const float POUND = 1.31; const float YEN = 0.0091; const float RUPEE = 0.014; const float EURO = 1.11; char c; int currValue; float exchangeValue; float finValue; int printValue; struct node {    int...

  • can someone please double check my code here are the requirements please help me fulfill the...

    can someone please double check my code here are the requirements please help me fulfill the requirements Using the material in the textbook (NumberList) as a sample, design your own dynamic linked list class (using pointers) to hold a series of capital letters. The class should have the following member functions: append, insert (at a specific position, return -1 if that position doesn't exist), delete (at a specific position, return -1 if that position doesn't exist), print, reverse (which rearranges...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT