Question

c++ programming question! I have a string sentence. "Hello how are you" What the best way...

c++ programming question! I have a string sentence. "Hello how are you" What the best way to Get each word from the sentence and assign it to a variable and then add that variable to a vector?

I won't know how many words the sentence has, only that they will be separated by a space " " delimiter. I want each word push_front(word) to the vector.

Thank you!!

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

#include <iostream>

#include <vector>

#include <ctime>

#include <string>

using namespace std;

int main(){

string line="hello world goodMorning";

int len = line.length();

vector<string> subArray;

// looping through the array

for (int j = 0, k = 0; j < len; j++) {

// if space found putting the string to vector

if (line[j] == ' ') {

string ch = line.substr(k, j - k);

k = j+1;

subArray.push_back(ch);

}

if (j == len - 1) {

string ch = line.substr(k, j - k+1);

subArray.push_back(ch);

}

}

// ...

for (std::vector<string>::const_iterator i = subArray.begin(); i != subArray.end(); ++i)

std::cout << *i << ' ';

return 0;

}

Add a comment
Know the answer?
Add Answer to:
c++ programming question! I have a string sentence. "Hello how are you" What the best way...
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
  • String variables/Selection & loop Write a complete Java program which prompts the user for a sentence on one line wh...

    String variables/Selection & loop Write a complete Java program which prompts the user for a sentence on one line where each word is separated by one space, reads the line into one String variable using nextline(), converts the string into Ubbi dubbi and displays the translated sentence. Ubbi dubbi works by adding ub before each vowel sound in a syllable. For example, hello becomes hubellubo. The word speak has the vowel sound ea, so in Ubbi dubbi it becomes spubeak....

  • Hi, I have C++ programming problem here: Problem: Please modify your string type vector in for...

    Hi, I have C++ programming problem here: Problem: Please modify your string type vector in for push_back() function as below: void push_back(string str) { // increase vector size by one // initialize the new element with str } In addition, the standard library vector doesn't provide push_front(). Implement push_front() for your vector. Test your code with the main function below. int main() {    vector v1(3);    cout<<"v1: ";    v1.print(); // this should display -, -, -    for...

  • I have a question regarding java programming. If I have this in a text file, "...

    I have a question regarding java programming. If I have this in a text file, " hey you <hello world> " How can I get it so that it only extracts the words in between the "< > " ? I am doing this with a stream and I know you can get it with the .map() function but I cannot figure out how to do it.

  • Hello I need a small fix in my program. I need to display the youngest student...

    Hello I need a small fix in my program. I need to display the youngest student and the average age of all of the students. It is not working Thanks. #include <iostream> #include <iomanip> #include <fstream> #include <vector> #include <algorithm> using namespace std; struct Student { string firstName; char middleName; string lastName; char collegeCode; int locCode; int seqCode; int age; }; struct sort_by_age { inline bool operator() (const Student& s1, const Student& s2) { return (s1.age < s2.age); // sort...

  • In C Sixth: Pig Latin (10 Points) For this part of the assignment, you will need...

    In C Sixth: Pig Latin (10 Points) For this part of the assignment, you will need to write a program that reads an input string representing a sentence, and convert it into pig latin. We'll be using two simple rules of pig latin: 1. If the word begins with a consonant then take all the letters up until the first vowel and put them at the end and then add "ay" at the end. 2. If the word begins with...

  • In C Programming: (100 pts) Write a program to find the words that have a specific...

    In C Programming: (100 pts) Write a program to find the words that have a specific character among the words of a string. Read a string and a character from the user and display all the words having that character on the screen. One way to do this is to find the words in one sentence and check if they have the required character in them. Another way is to find all the occurence of the character and then find...

  • Hello, I am struggling with this C# WPF application for one of my classes. I am...

    Hello, I am struggling with this C# WPF application for one of my classes. I am using visual studio to work on it. The description for the problem is as follows: Create an app that can open a text file. Each line of the file contains an arbitrary number of words separated by spaces. Display the count for how many words are in the line and the count how many non-space characters are in the line on the form /...

  • Part I. Representation of a String The primitive type char is used to store a single...

    Part I. Representation of a String The primitive type char is used to store a single character, whether it is a letter, number or special character (for example: space, tab, or new line). More often, we want to store a string of characters in a single variable. We can do this by using a variable of the class type String. A String variable can be initialized to a String literal (which is similar to how a char variable can be...

  • In Java please Only use methods in the purpose. Thank you The purpose of this assignment is to help you learn Java iden...

    In Java please Only use methods in the purpose. Thank you The purpose of this assignment is to help you learn Java identifiers, assignments, input/output nested if and if/else statements, switch statements and non-nested loops. Purpose Question 2-String variables/Selection & loops. (8.5 points) Write a complete Java program which prompts the user for a sentence on one line where each word is separated by one space, reads the line into one String variable using nextline), converts the string into Ubbi...

  • I need my c++ code converted to MASM (assembly language). The instructions below: write an assembly...

    I need my c++ code converted to MASM (assembly language). The instructions below: write an assembly program that does the following; 1. count and display the number of words in the user input string. 2. Flip the case of each character from upper to lower or lower to upper. For example if the user types in:   "Hello thEre. How aRe yOu?" Your output should be: The number of words in the input string is: 5 The output string is : hELLO...

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