CS 1723, Data Structures Second Hour Exam (25) 1. (a) Write two versions of a C function with prototype int digits(char *s); that will return the number of digits (0±9) in the string s. (The function digits() returns 0 in case s has no digits. To help you write digits(), you can use the C function isdigit(ch) which returns 0 if ch is not a digit and returns non-zero if ch is a digit.) For example, digits("Fahrenheit 451"); should return 3. You must write two versions of this function: one that uses array notation (with square brackets []) and another that uses pointer arithmetic (no use of array notation, no square brackets []). (b) Is it possible for a function like digits() to change the characters stored in the parameter passed to it, so that the change of the characters in the string will be reflected back in the calling function? (Yes/No) (c) Is it possible for a function like digits() to to change the address of the string passed to it as a parameter, so that the change to a string at a different address will be reflected back in the calling function? (Yes/No) (d) Is it possible for a function like digits() to change the characters stored in the parameter passed to it, so that there will more characters than were allocated in the calling function, and so that the change in the string will be reflected back in the calling function? (Yes/No) () 2. Consider the following program, involving an array of pointers to char: /* Weeks program */ #include void main(void) { char **p, **q; char *weeks[] = {"none", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday", NULL}; p = weeks; printf("Printing weeks[4]: %?\n", weeks[4] ); printf("Printing weeks[2][4]: %?\n", weeks[2][4] ); printf("Printing *(weeks + 3): %?\n", *(weeks + 3) ); printf("Printing *p: %?\n", *p ); printf("Printing **p: %?\n", **p ); printf("Printing *(*(p + 1) + 4): %?\n", *(*(p + 1) + 4) ); q = &weeks[2]; printf("Printing *q: %?\n", *q ); printf("Printing **q: %?\n", **q ); } For each of the eight cases above, say whether %? should be %c or %s. In each case say what string or character will be printed. (25) 3. Consider the following C code, with the code for the printlist() and for the insert() functions missing. You must supply the missing code for these functions. This is to be a linked list, and not an array, so you will need to use the malloc() function inside insert(). The insert() function should insert each new character at the beginning of the list, so that a simple printlist() function will print characters in the opposite order from the order in which they were inserted. (Thus printlist() should just move down the list, printing characters as it goes. You can print a newline at the end.) Insert must return a pointer to the head of the new list, so that it can be used as shown in the function below. /* List program, used in Problem 3 on Exam2 */ #include #include struct lnode { char ch; struct lnode *next; }; struct lnode *insert(char c, struct lnode *p); void printlist(struct lnode *); void main(void) { char c; struct lnode *list = NULL; while ((c = getchar()) != '\n') list = insert(c, list); printlist(list); } struct lnode *insert(char c, struct lnode *p) { /* INSERT CODE HERE */ } void printlist(struct lnode *p) { /* INSERT CODE HERE */ } runner% cc -o exam2_3 exam2_3.c runner% exam2_3 abcdefg gfedcba runner%