COS3711 Assignment 1 (COMPLETE GUIDELINE) 2025 Course
· Advanced Programming (COS3711)
Question 1 Write a console application that can be run from the command
line using the following forms: count // run without any parameters count //
pass one file name count fileT // pass more than one file name count –a –b
fileT // pass flags to change behaviour count –ab –c // pass flags in an
alternative way If no arguments are provided, then print a message
describing what arguments should be included. The application should, using
regular expressions, count the number of occurrences of each of the
following in the text files given. • If the –a flag is set, count the number of
words longer than 4 characters that start with a capital letter. There may be
further capital letters in the word. • If the –b flag is set, count the number of
words that are hyphenated. This hyphen should not be at the start or end of a
word. • If the –c flag is set, count the number of words that start and end on
the same character. • If the –d flag is set, count the number of words that do
not start with a vowel. Note that these words can start with any character,
and do not just have to start with alphabetic characters. If no flags are
provided, it is assumed that all counts should be performed. It is suggested
that you remove all whitespace at the start and end of the document, as well
are removing all punctuation (.,?!:; and so on) – try doing this using a regular
expression. Assume that there is only 1 space between words. Remember
that to set command line arguments in Qt Creator, click on Projects in the left
menu, click on the Run menu and enter the arguments in the Command line
arguments field. You can also click the Run in terminal check box.
Downloaded by Polar magnats () lOMoARcPSD| COS3711/Assignment 1/2025 3
See the screenshot below for an example of the output for the arguments
that were used above.
C++ console application that implements the requirements of your assignment
step-by-step, with support for multiple files and flags using regular expressions.
✅ Features Implemented: Parses flags (-a, -b, -c, -d)
Supports multiple files
Applies regex to:
, Remove punctuation
Split text into words
Counts according to:
-a: Words >4 letters starting with a capital
-b: Hyphenated words (not starting or ending with -)
-c: Words that start and end with the same letter
-d: Words that do not start with a vowel
If no flags provided → all checks performed
If no arguments → usage instructions
📌 Full Code (C++): cpp Copy Edit #include #include #include #include #include
#include
using namespace std;
// Helper function to read file content into a string string readFile(const string&
filename) { ifstream file(filename); if (!file) { cerr << "Error opening file: " <<
filename << endl; return ""; } return string((istreambuf_iterator(file)),
istreambuf_iterator()); }
// Function to clean punctuation using regex string cleanText(const string& text)
{ // Remove punctuation and trim whitespace string cleaned = regex_replace(text,
regex(R"([.,?!:;])"), ""); cleaned = regex_replace(cleaned, regex(R"(^\s+|\s+$)"), "");
return cleaned; }
// Function to split text into words (assumes one space between words) vector
splitWords(const string& text) { vector words; regex wordRegex(R"(\s+)");
sregex_token_iterator it(text.begin(), text.end(), wordRegex, -1);
sregex_token_iterator end; for (; it != end; ++it) { if (!it->str().empty())
words.push_back(it->str()); } return words; }
// Count functions bool isCapitalLongWord(const string& word) { return
word.length() > 4 && isupper(word[0]); }
bool isHyphenatedWord(const string& word) { return word.find('-') != string::npos
&& word[0] != '-' && word.back() != '-'; }
· Advanced Programming (COS3711)
Question 1 Write a console application that can be run from the command
line using the following forms: count // run without any parameters count //
pass one file name count fileT // pass more than one file name count –a –b
fileT // pass flags to change behaviour count –ab –c // pass flags in an
alternative way If no arguments are provided, then print a message
describing what arguments should be included. The application should, using
regular expressions, count the number of occurrences of each of the
following in the text files given. • If the –a flag is set, count the number of
words longer than 4 characters that start with a capital letter. There may be
further capital letters in the word. • If the –b flag is set, count the number of
words that are hyphenated. This hyphen should not be at the start or end of a
word. • If the –c flag is set, count the number of words that start and end on
the same character. • If the –d flag is set, count the number of words that do
not start with a vowel. Note that these words can start with any character,
and do not just have to start with alphabetic characters. If no flags are
provided, it is assumed that all counts should be performed. It is suggested
that you remove all whitespace at the start and end of the document, as well
are removing all punctuation (.,?!:; and so on) – try doing this using a regular
expression. Assume that there is only 1 space between words. Remember
that to set command line arguments in Qt Creator, click on Projects in the left
menu, click on the Run menu and enter the arguments in the Command line
arguments field. You can also click the Run in terminal check box.
Downloaded by Polar magnats () lOMoARcPSD| COS3711/Assignment 1/2025 3
See the screenshot below for an example of the output for the arguments
that were used above.
C++ console application that implements the requirements of your assignment
step-by-step, with support for multiple files and flags using regular expressions.
✅ Features Implemented: Parses flags (-a, -b, -c, -d)
Supports multiple files
Applies regex to:
, Remove punctuation
Split text into words
Counts according to:
-a: Words >4 letters starting with a capital
-b: Hyphenated words (not starting or ending with -)
-c: Words that start and end with the same letter
-d: Words that do not start with a vowel
If no flags provided → all checks performed
If no arguments → usage instructions
📌 Full Code (C++): cpp Copy Edit #include #include #include #include #include
#include
using namespace std;
// Helper function to read file content into a string string readFile(const string&
filename) { ifstream file(filename); if (!file) { cerr << "Error opening file: " <<
filename << endl; return ""; } return string((istreambuf_iterator(file)),
istreambuf_iterator()); }
// Function to clean punctuation using regex string cleanText(const string& text)
{ // Remove punctuation and trim whitespace string cleaned = regex_replace(text,
regex(R"([.,?!:;])"), ""); cleaned = regex_replace(cleaned, regex(R"(^\s+|\s+$)"), "");
return cleaned; }
// Function to split text into words (assumes one space between words) vector
splitWords(const string& text) { vector words; regex wordRegex(R"(\s+)");
sregex_token_iterator it(text.begin(), text.end(), wordRegex, -1);
sregex_token_iterator end; for (; it != end; ++it) { if (!it->str().empty())
words.push_back(it->str()); } return words; }
// Count functions bool isCapitalLongWord(const string& word) { return
word.length() > 4 && isupper(word[0]); }
bool isHyphenatedWord(const string& word) { return word.find('-') != string::npos
&& word[0] != '-' && word.back() != '-'; }