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

Program, Define a class polynomial with three private data members a, b and...

Define a class polynomial with three private data members a, b and c of type double to represent the coefficient of two degree polynomial(ax^2+bx+c). Include a constructor in a pol

Algorithm, make a marksheet of 2 student with 5 subject

make a marksheet of 2 student with 5 subject

Program to write c++ program, program to write superposition of waves using...

program to write superposition of waves using class and objects

Source code, processing two jobs through 2 machine

processing two jobs through 2 machine

What is b-tree, B-tree: A B-tree is an also called balanced m-way tree. A ...

B-tree: A B-tree is an also called balanced m-way tree. A node of the tree may have many records or key and pointers to children. It is also called as the balanced sort tree. It s

Converting base type to class type (char to string), Converting Base Type t...

Converting Base Type to Class Type (char to string)   class String { char *name; int length; public: String(void) {length =0; name = new char[length+1];

Stack, write a program in c language to implement stacks in a single array

write a program in c language to implement stacks in a single array

Decode the code, Smugglers are becoming very smart day by day. Now they hav...

Smugglers are becoming very smart day by day. Now they have developed a new technique of sending their messages from one smuggler to another. In their new technology, they are send

Output, #include void func(int num, b=5) { auto int total=0; static ...

#include void func(int num, b=5) { auto int total=0; static int sum=0; for ( int i=num; i>0 ; i--) total+=i; sum+=total; cout

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