Match Assignment for u0000000
The goal of this assignment 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” (always lowercase, no extra space) for each matching
argument and “no” (always lowercase, no extra space) 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 be in ANSI C, so that it compiles without
errors or extra flags on the CADE lab1-n.eng.utah.edu machines using the default gcc.
Your implementation must not depend on any libraries other than the standard C
library; please note that while "regex.h" and its associated functions are POSIX-standard,
they are not part of ANSI C and therefore not allowed in this
assignment. Also, yor implementation 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 -o matchlab matchlab.c
on a CADE lab machine builds your program as matchlab; your program should
work the same with and without debugging and optimization options such as -g or -O2. 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 the following, with nothing else before, after, or in between:
- any odd number of repetitions of the letter “c”;
- exactly one “=”;
- between 2 and 5 repetitions (inclusive) of the letter “r”;
- exactly one “,”; 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:
- ccc=rrrr,HCA
→ AAAAAAAAAAAA
- ccccccccc=rrr,GCE
→ EEEEEEEEEEEEEEEEE
- ccccccc=rrr,FEG
→ GGGGGGGGGGGGGGG
- c=rrr,G
→ GGGGGGG
- c=rrrr,FGE
→ EEEEEEEEEE
- c=rr,EEEHE
→ EEEEEEEEEE
- ccc=rrr,ECEBC
→ CCCCCCCCCCCCC
- ccccc=rrr,BFB
→ BBBBBBBBBBBBB
- ccccc=rr,ABC
→ CCCCCCCCCCCC
- c=rrr,BBFBE
→ EEEEEEEEEEE
Example arguments that do not match:
- .ccc=rrrr,HCA
- ccc=rrrr,HCA.
- e=rrr,ADBEA
- =vvvvvvv,EFA
- ccccccccc=wwwwwww_BFG
- ccccc,rr:H
- ccccccc=oo=B
- ccccccc:rr,E
- ccccccc=ppppppp,A
- ccccccc=rrrr:BGC
- ccccccc=rrrr_ADC
- ccccccc=rr=BFCHB
- rrr,B=ccccccc
- rrrr,cccccccccHFE=
-b mode
Match a sequence of the following, with nothing else before, after, or in between:
- 0 or more repetitions of the letter “g”;
- exactly one “,”;
- an odd number of uppercase letters — call this sequence X;
- 3 or more repetitions of the letter “w”;
- exactly one “_”;
- the same character sequence as X, but reversed; and
- between 1 and 3 (inclusive) decimal digits.
For matches, perform the following conversion:
- after each character, insert a digit that is the position of the character modulo 8.
Example arguments that match, followed by their conversions:
- ggggggg,BEFwwwwwwww_FEB310
→ g0g1g2g3g4g5g6,7B0E1F2w3w4w5w6w7w0w1w2_3F4E5B6371001
- ggggggggg,BCCwwwwwwww_CCB157
→ g0g1g2g3g4g5g6g7g0,1B2C3C4w5w6w7w0w1w2w3w4_5C6C7B0115273
- gg,Gwwwwwww_G866
→ g0g1,2G3w4w5w6w7w0w1w2_3G4856667
- gggg,HGDHFwwwwww_FHDGH189
→ g0g1g2g3,4H5G6D7H0F1w2w3w4w5w6w7_0F1H2D3G4H5168790
- ggggg,Ewwwwwww_E891
→ g0g1g2g3g4,5E6w7w0w1w2w3w4w5_6E7809112
- gggggggg,GEHwwwwwwww_HEG131
→ g0g1g2g3g4g5g6g7,0G1E2H3w4w5w6w7w0w1w2w3_4H5E6G7103112
- ggggggggg,FAFwwwwwwww_FAF794
→ g0g1g2g3g4g5g6g7g0,1F2A3F4w5w6w7w0w1w2w3w4_5F6A7F0719243
- gggg,Gwwwwwwww_G142
→ g0g1g2g3,4G5w6w7w0w1w2w3w4w5_6G7104122
- gggggg,GGGDHwwwwwwww_HDGGG338
→ g0g1g2g3g4g5,6G7G0G1D2H3w4w5w6w7w0w1w2w3_4H5D6G7G0G1323384
- ggggggggg,AEFwwwwwww_FEA843
→ g0g1g2g3g4g5g6g7g0,1A2E3F4w5w6w7w0w1w2w3_4F5E6A7804132
Example arguments that do not match:
- .ggggggg,BEFwwwwwwww_FEB310
- ggggggg,BEFwwwwwwww_FEB310.
- dd,ECDFBpppp_CF204
- ll,AEEBGyyyyyyy_GBEEA473
- gg:GFDBFwwwwwwwww_FBDFG348
- gggggg,GDBDFwwwwww:FDBDG373
- gggggggg,HEFCEttttttttt_ECFEH513
- g,FHBttttttttt_BHF452
- g,Ewwwwwww:E575
- ggg=FGCwwwwww_FGCFGCFGCFGC323
- gggg=Cwwwwwwwww_C423
- ggg,FDBrrrrrrrrr_BDF46
- 665wwwwwwwggggggggg_FCA,ACF
- _CHEGHHGEHCgggggggwwwwwwww720,
-c mode
Match a sequence of the following, with nothing else before, after, or in between:
- any odd number of repetitions of the letter “j”;
- exactly one “_”;
- an odd number of uppercase letters — call this sequence X;
- any odd number of repetitions of the letter “u”;
- exactly one “_”;
- the same characters as the odd-positioned characters in X; and
- between 1 and 3 (inclusive) decimal digits.
For matches, perform the following conversion:
- remove every “E” and “G”.
Example arguments that match, followed by their conversions:
- jjj_Euuuuu_748
→ jjj_uuuuu_748
- jjjjjjjjj_AAHu_A269
→ jjjjjjjjj_AAHu_A269
- jjjjjjj_Au_180
→ jjjjjjj_Au_180
- jjjjjjjjj_EBDu_B187
→ jjjjjjjjj_BDu_B187
- j_Eu_312
→ j_u_312
- j_Duuuuuuuuu_618
→ j_Duuuuuuuuu_618
- jjj_Cuuuuuuuuu_277
→ jjj_Cuuuuuuuuu_277
- jjj_EBHDHuuuuu_BD912
→ jjj_BHDHuuuuu_BD912
- j_Fu_629
→ j_Fu_629
- jjjjj_BGHu_G931
→ jjjjj_BHu_931
Example arguments that do not match:
- .jjj_Euuuuu_748
- jjj_Euuuuu_748.
- iiiiiii=CDHDAwwwwwwwww_DD886
- hh_HGGuuu_G892
- jjjjjjj_HBDuuuuu,B307
- j_BDDrr_D81
- jjj_HGAvvvvvvv=G998
- jjjjj_ACFEGqqqqqq_CE688
- aaaaaaaa_CCEuuuuuuu_C526
- j_FEEBHuuuuu_FEEBHFEEBHFEEBH736
- jjj_HHBFFppppp_HF498
- cc_Eu,952
- _Buuuuuuuuu505_jjjjjjj
- Huuuuu__jjjjjjjjj652
Examples
Assuming that your program is created as ./matchlab, here are some
example command-line uses (where “$” is a command-line prompt). We
expect your program to match this output exactly.
$ ./matchlab ccccccc=rrrr,GHG
yes
$ ./matchlab ccc=ppppppp:DAH
no
$ ./matchlab ccccccc=rrrr,GHG ccc=ppppppp:DAH
yes
no
$ ./matchlab -t ccccccc=rrrr,GHG
GGGGGGGGGGGGGGGG
$ ./matchlab -t ccc=ppppppp:DAH ccccccc=rrrr,GHG
GGGGGGGGGGGGGGGG
$ ./matchlab -t ccccccc=rrrr,GHG ccccc=rrrr,C
GGGGGGGGGGGGGGGG
CCCCCCCCCCCC
$ ./matchlab -t -b gg,Cwwwwwwwww_C606
g0g1,2C3w4w5w6w7w0w1w2w3w4_5C6670061
$ ./matchlab -c -t jjjjjjjjj_GEHuuuuuuu_E914
jjjjjjjjj_Huuuuuuu_914
$ ./matchlab -c jjjjjjjjj_GEHuuuuuuu_E914
yes