Question

In: Computer Science

C++ C++ We are prompting input from the users. Users input "I like the book "The...

C++

C++

We are prompting input from the users.

Users input "I like the book "The little prince" very much"

I want to separate this sentence by the order "I" "like" "the" "book" "the little prince" "very" "much". (Keep the quotation mark of little prince, because it is the way the user entered)

and adding this to a arraylist in c++  

Solutions

Expert Solution

#include <iostream>
#include <vector>

using namespace std;

vector<string> parseCSV(string s) {
        vector<string> result;

        // while string has a space
        while(s.find(" ") != string::npos) {
                int index = -1;
                bool insideQuote = false;
                for(int i=0; i<s.size(); i++) {
                        if(s[i] == '"') {
                                insideQuote = !insideQuote;
                        } else if(s[i] == ' ') {
                                if(!insideQuote) {
                                        index = i;
                                        break;
                                }
                        }
                }
                // break token from index.
                string token = s.substr(0, index);
                s = s.substr(index + 1);

                // remove space from start of token
                while(token != "" && token[0] == ' ') {
                        token = token.substr(1);
                }
                if(token != "") {
                        result.push_back(token);
                }
        }
        
        while(s != "" && s[0] == ' ') {
                s = s.substr(1);
        }
        if(s != "") {
                result.push_back(s);
        }

        return result;
}

int main() {
        string s = "I like the book \"The little prince\" very much";
        vector<string> tokens = parseCSV(s);

        for(string t: tokens) {
                cout << t << endl;
        }
}
**************************************************

Thanks for your question. We try our best to help you with detailed answers, But in any case, if you need any modification or have a query/issue with respect to above answer, Please ask that in the comment section. We will surely try to address your query ASAP and resolve the issue.

Please consider providing a thumbs up to this question if it helps you. by Doing that, You will help other students, who are facing similar issue.


Related Solutions

I am working on making a simple grade book that will take user input like name...
I am working on making a simple grade book that will take user input like name and grade and then return the name and grade of multiple inputs when the person quits the program. Here is the code that I have been working on. This is in Python 3. I can get one line to work but it gives me an error. Here is what it is supposed to look like: These are just examples billy 100 greg 60 jane...
I'm making a python program that checks the users input. If the users input does not...
I'm making a python program that checks the users input. If the users input does not match the rules then the program will output "No". If the users input does match the rules it will output "Yes". The rules are : at least 5 uppercase letters at least 5 lowercase letters at least 5 numbers No more than 20 characters in total I have managed to meet these conditions in individual python files but not in one. Ideally without importing...
I'm making a python program that checks the users input. If the users input does not...
I'm making a python program that checks the users input. If the users input does not match the rules then the program will output "No". If the users input does match the rules it will output "Yes". The rules are : 5 uppercase letters 5 lowercase letters 5 numbers First letter must be capitilized The total characters must be 15 I have managed to meet these conditions in individual python files but not in one. Ideally without importing anything, still...
Hi, I would like to know the answer in the following book. Investments. 11 Edition, from...
Hi, I would like to know the answer in the following book. Investments. 11 Edition, from chapter 1 - Problem Sets: 6 question: Q6) Suppose housing prices across the world double. a. Is society any richer for the change? b. Are homeowners wealthier? C. Can you reconcile your answers to (a) and (b)? Is anyone worse off as a result of the change? I don't have any details other than my textbook. Please let me know what else do you...
describe your product idea and theme describe potential users list input from potential users that could...
describe your product idea and theme describe potential users list input from potential users that could be used to guide your design explain why the proposed data needs are relevant to the product design Your final document should include the following: A brief description of your potential product A profile of your target users A discussion of what data you need from your users and how it will help guide your decisions about your product (a good idea is for...
Hello, I stuck doing c++ program The program will be recieved from the user as input...
Hello, I stuck doing c++ program The program will be recieved from the user as input name of an input file and and output.file. then read in a list of name, id# and score from input file (inputFile.txt) and initilized the array of struct. find the student with the higher score and output the students info in the output file obtain the sum of all score and output the resurl in output file. prompt the user for a name to...
C++ programming Complete the code to input and store all the information in the structure Book:...
C++ programming Complete the code to input and store all the information in the structure Book: title, author, year and pages. Complete the function print() with arguments and code to print a Book variable in the following format: Book title: <title> Book author: <name> Published in <year> Number of pages: <pages> #include <iostream> using namespace std; struct Book { string title; string author; string year; int pages; }; void print(/*arguments*/) { //Complete function } int main() { //Declare structure variable...
For this problem we have to put it in like the book shows examples, for instance...
For this problem we have to put it in like the book shows examples, for instance how they have 3 colums one with physical units direct materials and conversion cost with the lables for things on the left saying where they go and what their for. I need help on this, its hard for me to understand and i would really appriciate it if the person who works on this problem could explain to me what and how you got...
There is a new input technique for touch-screen devices that allows users to input a word by sliding a finger from letter to letter.
what is the test static? what is the critical value? what is the p value?There is a new input technique for touch-screen devices that allows users to input a word by sliding a finger from letter to letter. The user's finger is only removed from the keyboard between words. The developers claim that typing speed using this new input technique is faster when compared with traditional touch-screen keyboards. The accompanying data table shows the typing speeds of 10 individuals who...
I am trying to solve a c++ problem over c strings where I have to input...
I am trying to solve a c++ problem over c strings where I have to input an email address and check if it is valid. After completing my code I keep getting errors that my program will not run, specifically the lines with my for loops. Can you please look at my code and tell me what is wrong and how I can fix the code? I included below the assignment instructions and my current code. Assignment Instructions Write a...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT