Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write a C++ program? which extracts words inside the parentheses from the text,

ID: 3935776 • Letter: W

Question

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

**The content of "input.txt":

Txxxxx University (TU) is a Public, Non-Sectarian, University established in 1884. The campus is located in Txxxxx, Mxxxxxxx, USA and hosts 31,960 (2012) students with an endowment of $66.2 million (2010).

**Use this program:

**What To Use:

***The Output Should Look Like:

Text: Txxxxx University (TU) is a Public, Non-Sectarian, University established in 1884. The campus is located in Txxxxx, Mxxxxxxx, USA and hosts 31,960 (2012) students with an endowment of $66.2 million (2010).

Extracted Word: TU, 2012, 2010

Thank you very much!!

#include #include #include #include vector» using namespace std; ] int main() 1 ifstream fin ("input.txt") string text; vector word; if (fin.fail)) cout

Explanation / Answer

// C++ code which extracts words inside the parentheses from the text, and stores the words into a vector.

#include <iostream>
#include <string.h>
#include <fstream>
#include <limits.h>
#include <stdlib.h>
#include <math.h>
#include <iomanip>
#include <stdlib.h>
#include <vector>


using namespace std;


int main()
{
ifstream fin ("1.txt");
string text;
vector<string> word;

if(fin.fail())
{
cout << "Unable to open file." << endl;
return -1;
}

getline(fin,text);

int i = 0;
int start_position;
int end_position;
string substr;
int length ;

while(i < text.size())
{
// find start and end position of parantheses and then find the substring between them
start_position = text.find("(",i);
end_position = text.find(")",i+1);
length = end_position - start_position -1;

substr = text.substr(start_position+1,length);
  
// push it to the vector
word.push_back(substr);

i = i + end_position;
}

cout << "Text: " << text << endl;
cout << "Entracted word: ";
for (int i = 0; i < word.size() -1; ++i)
{
cout << word[i] << ", ";
}
cout << word[word.size()-1] << endl;

fin.close();

return 0;
}


/*
input.txt:
Txxxxx University (TU) is a Public, Non-Sectarian, University established in 1884. The campus is located in Txxxxx, Mxxxxxxx, USA and hosts 31,960 (2012) students with an endowment of $66.2 million (2010).

output:
Text: Txxxxx University (TU) is a Public, Non-Sectarian, University established in 1884. The campus is located in Txxxxx, Mxxxxxxx, USA and hosts 31,960 (2012) students with an endowment of $66.2 million (2010).
Entracted word: TU, 2012, 2010


*/

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote