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
  • CONVERT THE FOLLOWING C/C++ PROGRAM INTO JAVA: //LinkedString.h #pragma once #include<iostream> #include<string> using namespace std; //declare...

    CONVERT THE FOLLOWING C/C++ PROGRAM INTO JAVA: //LinkedString.h #pragma once #include<iostream> #include<string> using namespace std; //declare a node datastruct struct Node {    char c;    Node *next; }; class LinkedString {    Node *head; public:    LinkedString();    LinkedString(char[]);    LinkedString(string);    char charAt(int) const;    string concat(const LinkedString &obj) const;    bool isEmpty() const;    int length() const;    LinkedString substring(int, int) const;    //added helper function to add to linekd list private:    void add(char c); };...

  • Your task is to complete the following function/functions: 1. Given a position in the linked list,...

    Your task is to complete the following function/functions: 1. Given a position in the linked list, delete the node at that position.(Silver problem - Mandatory ) 2. Print the sum of all negative elements in the linked list.(Gold problem) If you want, you can refer to the the previous recitation manual (which was on Linked Lists) to implement node deletion. #include <iostream> using namespace std; //----------- Define Node --------------------------------------------------- struct Node{ int key; Node *next; }; //----------- Define Linked List...

  • #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...

  • Hi I got error C2660 : 'valuein' : function does not take 2 parameters in visual...

    Hi I got error C2660 : 'valuein' : function does not take 2 parameters in visual studio 2013 this is my code so far #include <cstdlib> #include <list> #include <iostream> using namespace std; struct node {    int data;    node* next; }; void initialize(node*p); void insert(int data,int n); void remove(int b,int pos); bool empty(node*); void length(); bool valuein(int c); int reportvalue(int value); void ptintlist(); int menu(); int main() {    struct node*head=NULL;    char commands;    int value;   ...

  • 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");...

  • 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,...

  • 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...

  • This is just a partial C++ code. I am having a problem with the getline(cin, variable)...

    This is just a partial C++ code. I am having a problem with the getline(cin, variable) statement of my code. Please run the code for yourself and see that it doesn't let you enter the full name. It skips the prompt to enter the name and goes straight to the next cout statement. I can't use cin alone because I need to capture and store both the first name followed by space followed by last name. I was trying to...

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