H06 Practice Soln

H06 Practice Soln - CS107 Handout#6 J Zelenski Solutions to...

Info iconThis preview shows pages 1–2. Sign up to view the full content.

View Full Document Right Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: CS107 Handout #6 J Zelenski Oct 26, 2009 Solutions to midterm practice Midterm Exam: Friday, Oct 30 11am-12:15pm Location still TBD (watch website for announcement) Problem 1: C-strings a) A C-string is not limited to a length of <= 255 characters. b) A length of a P-string is available in O(1) time, not requiring O(length) like a C-string. c) char *PtoCString(char pstr) { int len = (unsigned char)pstr[0]; char *cstr = malloc(len+1); strncpy(cstr, pstr + 1, len); // does NOT append \0 in this case cstr[len] = '\0'; return cstr; } Problem 2: Client use of generic interfaces char *LoadMap(FILE *fp, CMap *cm) { char key[100], value[100]; while (fscanf(fp, "%99s %99s\n", key, value) == 2) { char *vp = strdup(value); CMapPut(cm, key, &vp); } char *longest = NULL; CMapMap(cm, FindLongestValue, &longest); return longest; } void FindLongestValue(char *key, void *value, void *data) { char *maxStr = *(char **)data; char *cur = *(char **)value; if (maxStr == NULL || strlen(cur) > strlen(maxStr)) *(char **)data = cur; } – 2 –...
View Full Document

{[ snackBarMessage ]}

Page1 / 2

H06 Practice Soln - CS107 Handout#6 J Zelenski Solutions to...

This preview shows document pages 1 - 2. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online