COP 3223H meeting -*- Outline -*- * "Friday" problems on C loops and arrays ** string problems *** mystrlen() ------------------------------------------ FOR YOU TO DO Without using strlen() (from ), write, in C, a function int mystrlen(char s[]); that return the number of chars in s up to the first null char ('\0'). Assume that there is a null char in s. The following are tests using tap.h: ok(mystrlen("") == 0); ok(mystrlen("c") == 1); ok(mystrlen("fiddle") == 6); ok(mystrlen("Madam I'm Adam!") == 15); ------------------------------------------ Note, strlen in is like this, but returns a size_t instead of an int. *** is_substring() This uses the type _Bool from ------------------------------------------ FOR YOU TO DO Without using strstr(), write, in C, a function _Bool is_substring(char sought[], char str[]) which returns true just when sought is a substring of str. That is, when there is some index i into str such that sought[0] == str[i], sought[1] == str[i+1], ..., and sought[sought_len-1] == str[sought_len-1] and all the indexes involved are legal for both sought and str. Assume that both sought and str are null-terminated strings. The following are tests using tap.h: ok(is_substring("abcd", "xabcd")); ok(is_substring("", "asdlfjlkfa")); ok(is_substring("fjl", "asdlfjlkfa")); ok(!is_substring("abba", "asdlfjlkfa")); ok(is_substring("Miss", "Mississippi")); ok(!is_substring("Fla.", "Mississippi")); ok(is_substring("Miss", "I love Mississippi")); ok(!is_substring("Miss", "I love Florida.")); ok(!is_substring("Miss", "mississippi")); ok(is_substring("Miss", "Thanks, Miss")); ok(is_substring("dog", "x dog")); ok(is_substring("lazy dog", "The quick brown fox jumped over the lazy dog")); ok(is_substring("brown fox", "The quick brown fox jumped over the lazy dog")); ok(!is_substring("Drump", "The quick brown fox jumped over the lazy dog")); ok(!is_substring("Flabergasted", "Flaber")); ------------------------------------------ Hint: it's helpful to write a helping function occurs_at() See is_substring.c in this directory. If there is more time, write this in Python without using in (or not in) or the .find() method. ** standard array problems ------------------------------------------ FOR YOU TO DO In C, write a function int arr_min(int a[], int sz) that takes an array of ints, a, of size sz, and returns the minimum element in a. The following are tests that use tap.h: int a3223[] = {3, 2, 2, 3}; ok(arr_min(a3223, 4) == 2); int a13223[] = {1, 3, 2, 2, 3}; ok(arr_min(a13223, 5) == 1); int a5041[] = {5, 0, 4, 1}; ok(arr_min(a5041, 4) == 0); int a999[] = {999}; ok(arr_min(a999, 1) == 999); ------------------------------------------ Hint: it may be helpful to write a min function. Can write this in Python also.