Hello, OnlineGDB Q&A section lets you put your programming query to fellow community users. Asking a solution for whole assignment is strictly not allowed. You may ask for help where you are stuck. Try to add as much information as possible so that fellow users can know about your problem statement easily.

Write a C++ program that extracts words inside the parentheses from the text, and stores the words into a vector.

0 votes
asked Feb 17, 2022 by Shane Schafer (120 points)
So below here I have the base of a program which reads text from a file, extracts the words inside the parantheses, and stores it into a vector. I've been stuck at this for a while now and would really appreciate any help with it. I can get the text from the file into the vector but I'm at a loss of extracting all the words inside the parantheses into another vector.

#include <iostream>

#include <fstream>

#include <string>

#include <vector>

using namespace std;

int main() {

ifstream fin("Lab8_2data.txt");

string text;

vector<string> word;

if(fin.fail()){

cout << "Unable to read the file.";

return -1;

}

getline(fin, text);

// you need to implement from here

fin.close();

return 0;

}

1 Answer

0 votes
answered Feb 22, 2022 by Peter Minarik (86,160 points)

You can use the std::string::find() function to find where a parenthesis opens and where it ends. Then you just need to grab all the characters between the opening and closing parentheses via std::string::substr().

You can find examples with the above links (I deliberately did not disclose the whole solution in C++ for you, thus you can learn something while dealing with the problem).

Come back with your attempt to solve the problem if you get stuck!

Good luck!

Welcome to OnlineGDB Q&A, where you can ask questions related to programming and OnlineGDB IDE and and receive answers from other members of the community.
...