Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

#include #include #include #include using namespace std; /* Create bool function that checks for a word and see if it matches the conditions */ bool

#include #include #include #include

using namespace std;

/* Create bool function that checks for a word and see if it matches the conditions */

bool isWordc(string word, char special)

{ /* If word the length of the word is 2 Return False */

if (word.length() < 2) { return false;

}

/* If the first letter of word is not a special char return false */

if (word[0] != special) {

return false;

}

/* Create a for loop that checks the letters of the words starting from the 2nd letter */

for (int i = 1; i < word.length(); i++) {

/* Create if statement that checks if the following letters in the word are not alphanumeric or not underscores */

if (!isalnum(word[i]) && word[i] != '_')

{

//If statement is true return false return false; }

}

//When conditions met return true return true; }

/* Create function that checks for special character from a file */ int countWordsAndTypes(string filename, char special)

{

/* Create an ifstream to check contents of a file */ ifstream infile(filename);

/* If the file does not open */ if (!infile)

{ /* Print out Cannot open the file with the filename */ cout << "CANNOT OPEN THE FILE " << filename << endl;

return 0; }

/* Create a counter that counts each word that is satisfied by the isWordc bool function */ int count = 0;

/* Create a string name line that reads each line of the file */

string line;

/* Crate a while loop to check each line of a file */

while (getline(infile, line))

{ /* Create a stringstream that checks for contents in each line */

istringstream ss(line);

/* Create string word that checks each word of a file */

string word;

/* Create a while loop that checks each word of a line */ while (ss >> word) { /* If the word has the special character and follows the conditions of the bool function isWordc */ if (isWordc(word, special))

{ /* Increnment the word that meets the conditions */

count++; } } }

//close the file infile.close();

//return the increment number of words return count; }

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

{ /* Check if the number of arguments is less than 2 */ if (argc < 2) {

// Print out no specified input file name cout << "NO SPECIFIED INPUT FILE NAME. " << endl; return 0; } ifstream infile(argv[1]); if (!infile.is_open()) { cout << "CANNOT OPEN THE FILE " << argv[1] << endl; return 0; }

//make the first arugment value be set //to the name of the file string filename = argv[1];

/* Set int type1 to check for the words of a file that start with a '$' char and return the counter */ int type1 = countWordsAndTypes(filename, '$');

/* Set int type1 to check for the words of a file that start with a '@' char and return the counter */

int type2 = countWordsAndTypes(filename, '@');

/* The sum of type1 and type2 prints out all the words of the file */ int all = type1 + type2;

//If all equals 0 if (all == 0) { //Print out file is empty cout << "File is empty." << endl; return 0; }

//if the first argument command equals 1 if (argc == 1)

{ //Print out there is no flag cout << "noflag" << endl; return 0; }

//If the argument command equals 2 if (argc == 2) { /* Print out the all the words, and type 1 and type 2 words */ cout << "noflag" << endl; return 0; }

//Create string flag that equals the //2nd arguement value string flag = argv[2];

//If flag equals -all if (flag == "-all") { /* Print out the all the words, and type 1 and type 2 words */ cout << "Total Number of words: " << all << endl; cout << "Number of Type 1 names: " << type1 << endl; cout << "Number of Type 2 names: " << type2 << endl; }

//Else if flag is equal to command "-type1" else if (flag == "-type1") { //Print out type1 names cout << "Number of Type 1 names: " << type1 << endl; } //Else if flag is equal to command "type2" else if (flag == "-type2")

{ //Print out type2 names cout << "Number of Type 2 names: " << type2 << endl; }

//else statement prints out not recognized flag else { cout << "UNRECOGNIZED FLAG " << argv[2] << endl; } return 0; }

What I need to do add to check if a another flag is entered and how to add the rest of the words

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored 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

Recommended Textbook for

More Books

Students also viewed these Databases questions

Question

3. What improvements might you suggest for the system?

Answered: 1 week ago