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

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)?


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:


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).


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:
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.

Posted Date: 3/4/2013 6:36:24 AM | Location : United States

Related Discussions:- Command line program to find name matches, Assignment Help, Ask Question on Command line program to find name matches, Get Answer, Expert's Help, Command line program to find name matches Discussions

Write discussion on Command line program to find name matches
Your posts are moderated
Related Questions
A small shop sells 280 different items. Every item is identified by a 3 - digit code. All items which start with a zero (0) are cards, all items which start with a one (1) are swee

C program to reverse the elements of array: #define rows 3 #define cols 3 void main() {                 int i=0,j=0;                 int arr[rows][cols];

THIS PROGRAM IS TO ADD THE TWO COMPLEX NO.S GIVEN BY THE USER  */ #include #include #include struct complex     {     int real;     int imag;     }; void main()  {  clrs

We started off taking about input, output, CPU and memory devices. Within C we need a method of storing large amounts of data in memory. We have used the idea of variables (pointer

Define Bitwise-Shift Operators? The shift operators perform suitable shift by operator on the right to the operator on the left. The right operator should be positive. The va

c program to construct a structure: struct sensus                 {                                 char name[30];                                 long int population;

there is a pebble merchant. he sells the pebbles,that are used for shining the floor.his main duty is to take the length of the room side but he sometines mistakes doing that and m


Write a C program to calculate the output Y for a given value of X for the following formula   Y=X 2 + 2X +3   #include stdio.h   void main()   {   char promt;   float y,x