Docsity

CSC 230 Homework 6 - Spring 2007, Assignments of Computer Science

Toggle sidebar
bg1
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.
s1->
c
o
p
p
e
r
c
a
b
l
e
\0
\0
\0
\0
\0
\0
s2->
o
p
t
i
c
a
l
f
i
b
e
\0
\0
\0
\0
\0
\0
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);
pf3
pf4

Partial preview of the text

Download CSC 230 Homework 6 - Spring 2007 and more Assignments Computer Science in PDF only on Docsity!

Homework 6 โ€“ CSC 230-001, Spring, 2007

Issued 3/1/07, due 4/2/ Note: Same comments as made in Homework 1 apply. 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. s1-> c o p p e r c a b l e \ 0 \ 0 \ 0 \ 0 \ 0 \ 0 \ 0 \ 0 s2-> o p t i c a l f i b e r \ 0 \ 0 \ 0 \ 0 \ 0 \ 0 \ 0 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 ", 7sizeof(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);
  1. For this question, you have to develop a single C source file that contains the source for a module that provides the various units of functionality described below. Function prototypes for these functions have been included in a header file called string_functions.h that is available on the class website. Another file, which includes a main function, will #include "string_functions.h" and will contain code that uses the functions in your code. A sample of such a file is available on the class website as string_test.c (more tests will be added in the grading version). Note: You must not use any strings library function (such as strcpy, strlen) in the code you submit. The easiest way to show this is to not include string.h. You must implement such functions yourself if you think they will be repeatedly useful. You are allowed to include ctype.h. Note: You should de-allocate any memory you allocate in your functions if they are not required to be present after the function call ends. (Parts (e), (f) below are obviously exceptions, because they are required to allocate strings that will persist after the call ends.) If your functions are called in a loop many times, there should be no memory leaks. For grading, your code will be will be compiled (for a student with Unity ID student) by: gcc โ€“ ansi โ€“ pedantic โ€“ Wall student_HW6_2.c string_tests.c - and is expected to compile with no warnings or errors. The functionality required is as follows. Unless otherwise mentioned, the original strings should always remain unchanged. a. Given a pointer s to a string and an integer number n, return 0 if the string terminates in n-1 characters or less (that is any of the first n characters pointed to by s are \ 0 ), 1 if not. Examples: "platform" 8 - 1 "platform" 9 - 0 b. Given two pointers to two strings, return 0 if they are exactly the same, 1 if they differ only in lower/upper case, 2 if they differ otherwise. Examples: "state" "state" - 0 "state" "State" - 1 "state" "carolina" - 2 c. Given a pointer to a string, modify the original string so that if there were any trailing blanks, they are removed. Return pointer to original string. d. Given a pointer to a string, modify the original string so that if there were any leading blanks, they are removed. Return pointer to original string. e. Given a pointer s to a string, and two integers p and q, create a new string which is the substring of s starting at position p and q characters long. The memory for the new string should be allocated within the function.
  1. For this question, you are required to develop a single C source file that contains the source for a module that implements a basic linked list. Function prototypes for these functions have been included in a header file called llist.h that is available on the class website. Another file, which includes a main function, will #include "llist.h" and will contain code that uses the functions in your code. A sample of such a file is available on the class website as list_tests.c (more tests will be added in the grading version). Note: You should de-allocate any memory you allocate in your functions if they are not required to be present after the function call ends. If your functions are called in a loop many times, there should be no memory leaks. For grading, your code will be will be compiled (for a student with Unity ID student) by: gcc โ€“ ansi โ€“ pedantic โ€“ Wall student_HW6_3.c list_tests.c The functionality required is described as comments in the file llist.h. If you are unfamiliar with linked lists, many good sources exist in texts on Data Structures, and on the web. Below is a few diagrams that may help with this particular task. list_element: list: Relationship: Insertion after a given element: list_element *listHead list_element *listTail float data[1000] int key list_element *next NULL NULL Given element New element