III. "Friday" problems on C loops and arrays A. string problems 1. downcase ------------------------------------------ FOR YOU TO DO In C, write a function void downcase(char str[]); that takes a string (which is assumed to be allocated and null-terminated), str, and modifies str so that every uppercase char in str (up to the null character) is changed to its corresponding lowercase equivalent, and all other characters are left alone. You may use the following library functions from : // checks if c is an uppercase char int isupper(int c); // return the lowercase equeivalent of c int tolower(int c); ------------------------------------------ ------------------------------------------ TESTING FOR DOWNCASE() // Gary Leavens, file test_downcase.c // Compile with: // gcc tap.c quantifiers.c downcase.c test_downcase.c -o test_downcase #include #include #include #include #include #include "tap.h" #include "quantifiers.h" extern void downcase(char str[]); // static pointer used for checking results char *gstr; // Ensures: result is true when gstr[i] is lower case if it's alphabetic static bool checkLower(int i) { return !isalpha(gstr[i]) || islower(gstr[i]); } // Ensures result is true if downcase worked correctly bool check_downcase(const char *str) { // add 1 to the length below to make sure the null char is copied size_t len = strlen(str) + 1; // To allow modification, need a local copy of str char temp[len]; strncpy(temp, str, len); // The char at index len is a null char: // ok(str[len] == '\0'); gstr = temp; printf("downcasing \"%s\"\n", temp); downcase(temp); printf(" ... result is \"%s\"\n", temp); ok(allIntsIn(checkLower, 0, len-1)); } int main() { plan(4); check_downcase("HI"); check_downcase("ABBA"); check_downcase("Madam, I'm Adam!"); check_downcase("The Rain in Spain Falls Mainly on the Plain!"); return exit_status(); } ------------------------------------------ B. array problems ------------------------------------------ FOR YOU TO DO In C, write a function: void print_histogram(const int freqs[], int sz); that takes an array of non-negative ints, freqs, and a non-negative int, sz, such that freqs has sz elements, and prints a row for each i < sz that has i (in 2 spaces) followed by a colon (:) and a space and freqs[i] asterisks. For example if freqs[i] == i, for 0<= i && i < 5, then the output would be: 0: 1: * 2: ** 3: *** 4: **** If freqs[0] == 3, freqs[1] == 2, freqs[2] == 2, freqs[3] == 3, freqs[4] == 4, freqs[5] == 0, freqs[6] == 2, freqs[7] == 10 then the output would be: 0: *** 1: ** 2: ** 3: *** 4: **** 5: 6: ** 7: ********** ------------------------------------------ How would you change the program to read in the freqs array? How would you change the program to read in the labels? C. an I/O problem ------------------------------------------ FOR YOU TO DO In C, write a program void print_triplets(); that reads from stdin an int, n, such that n >= 6, and prints on stdout triplets of ints of the form (a, b, c), such that a+b+c == n and 0 < a and a < b and b < c. One triplet should be printed per line. For example, print_triplets(6) prints: (1, 2, 3) and print_triplets(10) prints: (1, 2, 7) (1, 3, 6) (1, 4, 5) (2, 3, 5) ------------------------------------------