Homework 6 โ CSC 230-001, Spring, 2007
Issued 3/1/07, due 4/2/07
Note: Same comments as made in Homework 1 app ly. The answer to Q1 should be in a single text file.
The two source files you submit for Q2 and Q3 should be named according to the conventions, appending a
_2 and _3 at the end of the filenames, respectively.
1. The following code segment does some string manipulation with two pointers.
Assume that the memory allocations are successful. For every line of code after,
state what the contents of each of the 20-character buffers pointed to by s1 and
s2 will be. For example, after the strcpy calls immediately following the memory
allocations, the contents will be as given below.
char s1[20], s2[20];
for (i=0; i<20; i++) {
s1[i] = s2[i] = '\0';
}
strcpy (s1, "copper cable"); strcpy (s2, "optical fiber");
strncpy (s1+7, "link", 20);
sprintf (s1, "%s link", s2);
memcpy ((void *)s2, (void *)"copper ", 7*sizeof(char));
sscanf (s2, "copper%s", s1);
if (!strncmp (s1+7, s2+7, 6)) {
strcat (s2, s1+(strspn (s1, "abchefgh")));
}
strcpy (s1, strpbrk ("wireless", "uvwzyz"));
strcat (s1, " optical");
strcpy (s2+7, s1+9);