Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

#include #include #include #include using namespace std; bool isType1(string word) { return word[0] == '$' && isalpha(word[1]); } bool isType2(string word) { return word[0] ==

#include #include #include #include

using namespace std;

bool isType1(string word) {

return word[0] == '$' && isalpha(word[1]);

}

bool isType2(string word)

{

return word[0] == '@' && isalpha(word[1]);

}

bool isWord(string word) {

if (word.empty())

return false;

if (isType1(word) || isType2(word))

return true;

for (char c : word)

if (!isalnum(c) && c != '_')

return false;

return true;

}

int wordc(string line)

{

int count = 0;

string word;

stringstream ss(line);

while (ss >> word)

if (isWord(word))

count++;

return count;

}

int Type1(string line) {

int count = 0;

string word;

stringstream ss(line);

while (ss >> word); {

if (isType1(word))

count++; }

return count; }

int Type2(string line) {

int count = 0;

string word;

stringstream ss(line);

while (ss >> word) {

if (Type2(word))

count++; }

return count;

}

int main(int argc, char* argv[])

{

if (argc

{

cout

return 0;

}

ifstream infile(argv[1]);

if (!infile.is_open())

{

cout

return 0;

}

int words = 0, type1 = 0, type2 = 0;

string line;

while (getline(infile, line))

{

words += wordc(line);

type1 += Type1(line);

type2 += Type2(line);

}

if (words == 0)

{

cout

return 0;

}

if (argc == 2)

{

cout

}

else if (string(argv[2]) == "-all") {

cout

}

else if (string(argv[2]) == "-type1") {

cout

}

else if (string(argv[2]) == "-type2") {

cout

}

return 0; }

Checked for test cases for Infile3(img below) and it does not open properlyimage text in transcribed

image text in transcribed

\begin{tabular}{l|l} Line number & File contents \\ \hline 1 & # Various operations on scalar (string) variables. \\ 2 & \$fred = "Fred here"; \\ 3 & \$barney = 56; \\ 4 & \$sum = 10+ \$barney; \\ 5 & print 'The variable \$fred' ." contains \$fred. "; \\ 6 & print "Sum is \$sum. ; \end{tabular} The displayed output for executing the program with the "-all" flag would be as shown below

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access with AI-Powered Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions