Command line program to find name matches, C/C++ Programming

Assignment Help:

What's a six-letter word that has an e as its first, third, and fifth letter? Can you find an anagram of pine grave. Or how about a word that starts and ends with ant (other than ant itself, of course)?

Task

Your task is to write a program, named match, that can find such matches. The program is run from the command line, using this syntax:

match [-OPTION]... PATTERN [FILENAME]...

The program reads words from standard input (or from each of the named files in turn) and compares them with the pattern specified on the command line. A pattern is composed of letters and underscores ('_'). A letter matches itself; an underscore matches any character. For comparison purposes, the input line is considered as consisting of words separated by whitespace. If any of the input words match the specified pattern, the word is written to standard output; otherwise, nothing
is written for that word. The program stops when end of file is reached.

The process of matching is controlled by command-line options:
• With no options (or with the option -w), a word is matched if it matches the pattern in its entirety. Thus match c_t (or match -w c_t) would match cat or cut but not cater or scotch.
• The options -p and -s specify that a word is matched if the pattern occurs as either a prefix (at the beginning) or a suffix (at the end). Thus match -p cat would match catfish or cataclysmic, and match -s cat would match scat or bobcat.
• The option -a allows the match to occur anywhere within a word. Thus match -a cat would match vacate and amplification.
• The option -e specifies that a word is matched if it is embedded within the pattern. Thus match -e vacate would match cat and ate.
• Normally the letters in a word match only if they agree in case with the pattern. The option
-c makes the match independent of the case of the word. Thus match Cat would not match cat, whereas match -c Cat or match -c CAT would match cat or CAT or even cAt.
• Normally the pattern is matched exactly. However, if the option -j (for jumble) is specified, then the pattern is considered matched if any rearrangement of the pattern is matched. Thus match -j cat would also match act.
• The option -v reverses the sense of the match. The output will therefore consist of all of the words that do not match the pattern.
• The option -n takes an argument that specifies constraints on the length of the matching words. By default, any length word is permitted. However match -n3,8 -p cat would match words that begin with cat and that contain between 3 and 8 letters. As a special case, a minimum or maximum length of 0 (or an omitted length) indicates no restriction.

Thus, -n3,0 (or -n3) specifies words of 3 or more letters, -n0,8 (or -n,8) specifies words of 8 or fewer characters, and -n0,0 (or -n,) specifies words of any length.

All options come before any other arguments, but more than one option can be specified. The w, p, s, a, and e options are mutually exclusive. If more than one is specified, the last-specified option takes precedence. Thus match -p -a cat is equivalent to match -a cat. The other options are independent and can be specified in any order and any combination. For example match -a -j cat (or match -j -a cat) would match factor or antacid, and match -a -n7,7 cat would match Yucatan (the Mexican peninsula where the dinosaur-killing asteroid is thought to have landed 65 million years ago).

Hints

The getopt function (#include ) will greatly simplify the processing of option arguments. The function analyses the command-line parameters argc and argv for arguments that that begin with - and contain specified option letters, returning individual letters on successive calls and adjusting the variable optind to indicate which arguments it has processed. Consult getopt documentation for details.

The simplest strategy is to write argument processing code that sets control variables that will affect the operation of the program. For this assignment, suitable code would look something like this:
enum { WHOLE, PREFIX, SUFFIX, ANYWHERE, EMBEDDED } mode = WHOLE;
bool jumble = false;
bool ignore_case = false;
bool invert = false;
string length = "0,0";
int c;
while ((c = getopt(argc, argv, ":psaejivn:")) != -1) {
switch (c) {
case 'p': mode = PREFIX; break;
case 's': mode = SUFFIX; break;
case 'a': mode = ANYWHERE; break;
case 'e': mode = EMBEDDED; break;
case 'j': jumble = true; break;
case 'i': ignore_case = true; break;
case 'v': invert = true; break;
case 'n': length = optarg; break;
}
}
argc -= optind;
argv += optind;
At any point, variable optopt contains the current option letter, and for options that take arguments, variable optarg is a pointer to the argument string. Adjusting argc and argv after the loop completes effectively removes the option arguments, so that argc will indicate how many additional arguments remain and argv will contain the argument values.


Related Discussions:- Command line program to find name matches

Draw a picture using line - c program, Draw a picture using line: void...

Draw a picture using line: void main(void) {     int   driver = DETECT,mode;     int   x[10],y[10];     int   x_center = 360, y_center = 180, rad = 100;     int

Mat lab programming, MAT LAB programming Project Description: Just fo...

MAT LAB programming Project Description: Just for who are PROFESSIONAL IN MATLAB i have simulation and i would like to simulate the equation in ,and test the all simulatio

Do i require checking for null before delete p?, No. it's not required T...

No. it's not required The C++ language guarantees that delete p will do nothing if p is equivalent to NULL. As you might get the test backwards, and as most testing methodologie

Last ant on rod, There are ''n'' ants on a ''n+1'' length rod. The ants are...

There are ''n'' ants on a ''n+1'' length rod. The ants are numbered from 1 to n and are initially placed at positions starting from position 1 till position n. They are moving eith

Define the bitwise-exclusive-or operator, Define the Bitwise-Exclusive-OR O...

Define the Bitwise-Exclusive-OR Operator: ^: The bitwise-exclusive-OR operator (^) compares every bit of its first operand to the corresponding bit of its second operand. If on

PLS URGENT HELP WITH C++, Pls i only need help with program 2. #include ...

Pls i only need help with program 2. #include #include using namespace std; int main() { int FilingStatus; cout cout cout cout cout cout

Data type, what is virtual datatype

what is virtual datatype

Write a program to illustrate passing structure to function, Write a Progra...

Write a Program to illustrate passing structure to function? # include struct customer { int id; char name[15]; }; void func_struct(struct customer); main() { struct c

Write Your Message!

Captcha
Free Assignment Quote

Assured A++ Grade

Get guaranteed satisfaction & time on delivery in every assignment order you paid with us! We ensure premium quality solution document along with free turntin report!

All rights reserved! Copyrights ©2019-2020 ExpertsMind IT Educational Pvt Ltd