Computer Science 3510
Data Structures
Summer 1999
Exam 1
This is a closed book test. You may use one sheet of prepared notes. You have 90 minutes. Answer all of the questions.
This function works by copying each string, replacing lower case letters by upper case letters, and then using library function strcmp to tell whether the resulting strings are the same. (strcmp(s,t) returns 0 if strings s and t are equal, -1 if s comes before t in alphabetical order, and 1 if s comes after t in alphabetical order.) The function also uses function toupper, which converts a lower case letter to upper case, and returns all other characters unchanged. So toupper('a') = 'A' and toupper(':') = ':'.
There is a serious mistake in this function. Explain what the mistake is, and rewrite the function to avoid the mistake. You might need to make more than one change, but try to keep the spirit of the method instead of choosing a completely different method. Make sure the external behavior of the function is correct.
bool sameUpper(char* x, char* y) { char *cpyx, *cpyy; int xlen = strlen(x); int ylen = strlen(y); int i; if(xlen != ylen) return 0; for(i = 0; i <= xlen; i++) { cpyx[i] = toupper(x[i]); cpyy[i] = toupper(y[i]); } if(strcmp(cpyx, cpyy) == 0) return 1; else return 0; }
char* stutter(const char *s)