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

Padovan string, padovan string generation till 40

padovan string generation till 40

Explain the macros, Explain the Macros? Preprocessor' is a translation ...

Explain the Macros? Preprocessor' is a translation stage that is applied to your source code before the compiler proper gets its hands on it. Usually the preprocessor performs

Write function that take array as argument, Write a function that takes an ...

Write a function that takes an array as the argument and returns the second largest element. Bonus (+5): Write a function that takes an array and a number n as arguments and return

Algorithm, write a pseudo code for computing sin(x) using sentinel control ...

write a pseudo code for computing sin(x) using sentinel control loop

Using only arrays, write c program to do the following : -fill 2 dimensiona...

write c program to do the following : -fill 2 dimensional array (square matrix array with size=4). -ask the user to enter any integer number and add this number to the diagonals -o

Chelo, need some help with finishing a program

need some help with finishing a program

Online tutor, Given an char variable last that has been initialized to a lo...

Given an char variable last that has been initialized to a lowercase letter, write a loop that displays all possible combinations of two letters in the range ''a'' through last. Th

MCQ, in a multilist organisation

in a multilist organisation

Explain the pointer types, Pointer Types Pointer holds the address of a...

Pointer Types Pointer holds the address of an object, permitting for the indirect manipulation of that object. They are used in creating linked data structures like lists, tree

Explain access privileges, Access privileges 1. If the designer of the ...

Access privileges 1. If the designer of the base class needs no one, not even a derived class to access a member, then that member should be made private. 2. If the designer

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