Question: // Process command line arguments for a wc-like program // @param argc: number of command line arguments, including the program name // @param argv: array

// Process command line arguments for a wc-like program // @param argc: number of command line arguments, including the program name // @param argv: array of pointers to strings, one for each argument // @param cflag: address of a 0/1 integer variable - an output of the function // @param wflag: address of a 0/1 integer variable - an output of the function // @param nflag: address of a 0/1 integer variable - an output of the function // @param lflag: address of a 0/1 integer variable - an output of the function // @param outfile: address of a FILE * variable - an output of the function // @param name_len: address of an int variable - an output of the function

void parse_args(int argc, char *argv[], int *cflag, int *wflag, int *nflag, int *lflag, FILE **outfile, int *name_len) { // using the getopt library routine, process the command line // arguments given by argc and argv, looking for these options: // -c, -w, -n, -l: set the corresponding flag's int variable to 1 // -o name: try to open for output a FILE with the given name; if that // fails, print to stderr "Error opening output file: xxx " where // xxx is the name of the file, then exit (not return) // -f nnn: set the name_len's int to the integer value given by nnn; // you may use the atoi library function to do the necessary conversion // Default values: // If none of -c, -w, -n, and -l are given, set cflag, wflag, and lflag // (i.e., set the int variable to 1), and unset lflag (value 0). It is // probably easiest to (1) set all the flags' ints to 0 at the beginning; // (2) set individual flags as they are encountered, also remembering if // any have been set; and (3) check for the default case after the // argument processing loop. // If -o is not given, the default output FILE * is for outfile is stdout. // If -f is not given, the default value for name_len is 20. // For outfile and name_len, it is probably easiest to set their default at // the beginning. That way no later check is required. return; }

// Suppose we wish to count maximum length contiguous // sequences of vowels in a string. This pattern of // code can do that. Make sure you understand it before // applying it in the two functions below. In at least // one of the cases it will need adaptation beyond changing // just the string of characters to match against. // Note how it succinctly checks for the "s is NULL" case. // Note also that experienced C programmers often omit // != 0 from a test, since it is redundant ... int count_vowel_seqs(const char *s) { int count = 0; const char *vowels = "aeiouy"; int temp; while (s && *s) { // skip if null, and stop at end of string s += (temp = strspn(s, vowels)); // temp gets match length s += strcspn(s, vowels); count += (temp != 0); } return count; }

// Count words in a string. A "word" is a maximum length // contiguous sequence of characters that does not include // any of the delimiters space, tab, or newline. // @param s: The string for which the word count is desired // @result: The number of words in s; returns 0 if s is NULL int count_words(const char *s) { int count = -1; return count; }

// Count numbers in a string. A "number" is a maximum length // contiguous sequence of the digits 0 through 9. // @param s: The string for which the word count is desired // @result: The number of words in s; returns 0 if s is NULL int count_numbers(const char *s) { int count = -1; return count; }

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!