Match Lab for u0000000
The goal of this lab is to give you some practice with C on a problem
that is algorithmically straightforward but difficult to get right on a first try.
With any luck, you'll have a bug along the way, and then you get to practice using GDB.
Your task is to implement a program that takes command-line arguments
and checks whether each argument matches a particular pattern. The pattern to check
is determined by a command-line flag: -a, -b, or -c, where the default
mode is -a. The patterns for each flag are described below.
By default, your program should print “yes” for each matching
argument and “no” for each non-matching argument, each on a separate line.
If the -t flag is provided,
then your program should instead print nothing for non-matching argument and
it should print a converted value for each matching argument. Each pattern mode
has an associated conversion, as described below.
At most one of -a, -b, or -c will be provided. A -t
flag can be provided before or after any of -a, -b, and -c.
All flags (i.e,. up to two of them) precede other command-line arguments. A non-flag
argument never starts with -. All arguments are in ASCII.
See the end of this page for example command-line uses of your program. Neither
the examples there nor the examples for indvidual patterns are meant to be exhaustive.
Constraints: Your implementation must not depend on any libraries other than the standard C
library. Also, it must not use multiplication, division, or modulo operators. (Bitwise operations offer
alternatives to those operations.) You should hand in one
matchlab.c file so that gcc -O2 -o matchlab matchlab.c
on a CADE lab machine builds your program as matchlab. Hand in via
Canvas.
Important! Make sure the uID listed at the top of the page is correct,
because this assignment (and its correct answer) is uID-specific.
Where the descriptions below say “even position” or “odd position”,
counting is from 0, as in C. So, a first character is in an even position.
-a mode
Match a sequence of (with nothing else before or after)
- any odd number of repetitions of the letter “c”;
- between 2 and 4 repetitions (inclusive) of the letter “u”; and
- an odd number of uppercase letters.
For matches, perform the following conversion:
- replace each character by the last one.
Example arguments that match, followed by their conversions:
- cuuuGBE
→ EEEEEEE
- cccuuuHCA
→ AAAAAAAAA
- cccccccccuuuGCE
→ EEEEEEEEEEEEEEE
- cccccccuuFEG
→ GGGGGGGGGGGG
- cuuG
→ GGGG
- cuuuFGE
→ EEEEEEE
- cuuEEEHE
→ EEEEEEEE
- cccuuECEBC
→ CCCCCCCCCC
- cccccuuuBFB
→ BBBBBBBBBBB
- cccccuuABC
→ CCCCCCCCCC
Example arguments that do not match:
- cccccpppGABCA
- cnnnE
- cccccccccqFEF
- iiiiiiuuuA
- kkkkkkkkooooBFG
- fffffffffqqqqqqqHDD
- hhooooooooHDE
- jjjjjuuuA
- crrE
- cccccccrrrrrrGBF
-b mode
Match a sequence of (with nothing else before or after)
- any odd number of repetitions of the letter “j”;
- between 1 and 3 (inclusive) decimal digits — call this sequence X;
- any odd number of repetitions of the letter “o”;
- the same characters as the even-positioned characters in X; and
- an odd number of uppercase letters.
For matches, perform the following conversion:
- add one “E” before each “F”.
Example arguments that match, followed by their conversions:
- jjjjj212ooooooo22GFG
→ jjjjj212ooooooo22GEFG
- j845ooooooo85FFD
→ j845ooooooo85EFEFD
- jjj234ooooo24BAE
→ jjj234ooooo24BAE
- j970ooooooo90G
→ j970ooooooo90G
- jjj159ooooo19DCF
→ jjj159ooooo19DCEF
- jjjjj85ooo8FDA
→ jjjjj85ooo8EFDA
- jjjjj368ooooooo38DGD
→ jjjjj368ooooooo38DGD
- jjjjjjj890ooooooooo80C
→ jjjjjjj890ooooooooo80C
- jjjjjjj841ooooo81DBD
→ jjjjjjj841ooooo81DBD
- j180ooooooooo10HDA
→ j180ooooooooo10HDA
Example arguments that do not match:
- jjj887o788HAC
- jjjjjj906ooooooooo96DHFBB
- 58ooooo8ECDFB
- jjj507ooo0DGCHA
- hhhhhhh879ooooo89FHD
- jjjjjjjjj713t73C
- jjjjj949rrrrr99BDF
- jjjjj318s38CBGHH
- ggggg961o91GEADH
- jjj681yyyy61BDGDG
-c mode
Match a sequence of (with nothing else before or after)
- any number (including zero) repetitions of the letter “h”;
- between 1 and 3 (inclusive) decimal digits — call this sequence X;
- any odd number of repetitions of the letter “q”;
- the same characters as X repeated 3 times; and
- an odd number of uppercase letters.
For matches, perform the following conversion:
Example arguments that match, followed by their conversions:
- h729qqqqqqq729729729C
→ h729qqqqqqq729729729C
- hh437qqqqq437437437G
→ hh437qqqqq437437437G
- h661qqqqq661661661CDFAA
→ h661qqqqq661661661CDFAA
- hhh429qqqqqqq429429429F
→ hhh429qqqqqqq429429429F
- hhhh433qqqqq433433433DFBFD
→ hhhh433qqqqq433433433DFBFD
- 495qqqqqqqqq495495495H
→ 495qqqqqqqqq495495495H
- hh95qqqqq959595H
→ hh95qqqqq959595H
- hhhh726qqqqq726726726BBGGA
→ hhhh726qqqqq726726726BBGGA
- hhhh255qqqqqqq255255255GCB
→ hhhh255qqqqqqq255255255GCB
- h601q601601601G
→ h601q601601601G
Example arguments that do not match:
- h81qqq18DHGAB
- 547yyyyyyy547547547AAF
- ggggg187q187187187GHC
- b195xxx195195195HEC
- hhh158qqqqqqq158158158158EBG
- 127qqqq127127127H
- hh307q703EBBCB
- hh487vvvvvvv487487487H
- 946qqqqqq96EEBAD
- mmm786q786786786D
Examples
Assuming that your program is created as ./matchlab, here are some
example command-line uses (where “$” is a command-line prompt):
$ ./matchlab cccccuuuH
yes
$ ./matchlab cccccccsssssssssECB
no
$ ./matchlab cccccuuuH cccccccsssssssssECB
yes
no
$ ./matchlab -t cccccuuuH
HHHHHHHHH
$ ./matchlab -t cccccccsssssssssECB cccccuuuH
HHHHHHHHH
$ ./matchlab -t cccccuuuH cccccccccuuuF
HHHHHHHHH
FFFFFFFFFFFFF
$ ./matchlab -t -b jjjjjjjjj505ooooooo55FFC
jjjjjjjjj505ooooooo55EFEFC
$ ./matchlab -c -t hh98qqqqqqq989898ACC
hh98qqqqqqq989898ACC
$ ./matchlab -c hh98qqqqqqq989898ACC
yes