Ask Question, Ask an Expert

+61-413 786 465

info@mywordsolution.com

Ask C/C++ Expert


Home >> C/C++

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.

C/C++, Programming

  • Category:- C/C++
  • Reference No.:- M9525972

Have any Question?


Related Questions in C/C++

Question 1find the minimum and maximum of a list of numbers

Question: 1. Find the Minimum and Maximum of a List of Numbers: 10 points File: find_min_max.cpp Write a program that reads some number of integers from the user and finds the minimum and maximum numbers in this list. Th ...

Project - space race part a console Project - Space Race Part A: Console Implementation

Project - Space Race Part A: Console Implementation INTRODUCTION This assignment aims to give you a real problem-solving experience, similar to what you might encounter in the workplace. You have been hired to complete a ...

What are the legal requirements with which websites must

What are the legal requirements with which websites must comply in order to meet the needs of persons with disabilities? Why is maximizing accessibility important to everyone?

Software development fundamentals assignment 1 -details amp

Software Development Fundamentals Assignment 1 - Details & Problems - In this assignment, you are required to answer the short questions, identify error in the code, give output of the code and develop three C# Console P ...

1 implement the binary search tree bst in c using the node

1. Implement the Binary Search Tree (BST) in C++, using the Node class template provided below. Please read the provided helper methods in class BST, especially for deleteValue(), make sure you get a fully understanding ...

Assign ment - genetic algorithmin this assignment you will

ASSIGN MENT - GENETIC ALGORITHM In this assignment, you will use your C programming skills to build a simple Genetic Algorithm. DESCRIPTION OF THE PROGRAM - CORE REQUIREMENTS - REQ1: Command-line arguments The user of yo ...

There are several ways to calculate the pulse width of a

There are several ways to calculate the pulse width of a digital input signal. One method is to directly read the input pin and another method (more efficient) is to use a timer and pin change interrupt. Function startTi ...

Why do researcher drop the ewaste and where does it end

Why do researcher drop the ewaste and where does it end up?

Assignment word matchingwhats a six-letter word that has an

Assignment: Word Matching 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 ...

  • 4,153,160 Questions Asked
  • 13,132 Experts
  • 2,558,936 Questions Answered

Ask Experts for help!!

Looking for Assignment Help?

Start excelling in your Courses, Get help with Assignment

Write us your full requirement for evaluation and you will receive response within 20 minutes turnaround time.

Ask Now Help with Problems, Get a Best Answer

Why might a bank avoid the use of interest rate swaps even

Why might a bank avoid the use of interest rate swaps, even when the institution is exposed to significant interest rate

Describe the difference between zero coupon bonds and

Describe the difference between zero coupon bonds and coupon bonds. Under what conditions will a coupon bond sell at a p

Compute the present value of an annuity of 880 per year

Compute the present value of an annuity of $ 880 per year for 16 years, given a discount rate of 6 percent per annum. As

Compute the present value of an 1150 payment made in ten

Compute the present value of an $1,150 payment made in ten years when the discount rate is 12 percent. (Do not round int

Compute the present value of an annuity of 699 per year

Compute the present value of an annuity of $ 699 per year for 19 years, given a discount rate of 6 percent per annum. As