1 <!DOCTYPE HTML PUBLIC
"-//W3O//DTD W3 HTML 2.0//EN">
2 <!-- This collection of hypertext pages is Copyright 1995-7 by Steve Summit. -->
3 <!-- This material may be freely redistributed and used -->
4 <!-- but may not be republished or sold without permission. -->
7 <link rev=
"owner" href=
"mailto:scs@eskimo.com">
8 <link rev=
"made" href=
"mailto:scs@eskimo.com">
9 <title>Assignment #
6</title>
12 <H1>Assignment #
6</H1>
18 <B>Introductory C Programming
21 UW Experimental College
26 </B></p><p><a href=
"PS6.html">Assignment #
6</a>
27 <br><a href=
"PS5a.html">Assignment #
5 Answers
</a>
28 <br><a href=
"http://www.eskimo.com/~scs/cclass/notes/sx11.html">Class Notes, Chapter
11</a>
29 <br><a href=
"http://www.eskimo.com/~scs/cclass/notes/sx12.html">Class Notes, Chapters
12</a>
30 <a href=
"http://www.eskimo.com/~scs/cclass/notes/sx13.html">and
13</a>
31 <p><B>Reading Assignment:
32 </B></p><p><a href=
"http://www.eskimo.com/~scs/cclass/notes/sx10.html">Class Notes, Chapters
10</a>
33 <a href=
"http://www.eskimo.com/~scs/cclass/notes/sx11.html">and
11</a>
34 <p><B>Review Questions:
35 </B></p><OL><li>If we say
40 then what is
<TT>ip
</TT>?
42 <li>If
<TT>ip
</TT> is a pointer to an integer, what does
<TT>ip++
</TT> mean?
48 <li>How much memory does the call
<TT>malloc(
10)
</TT> allocate? What if
49 you want enough memory for
10 <TT>int
</TT>s?
53 int *ip =
&c; /* WRONG */
55 is in error; you can't mix
<TT>char
</TT> pointers and
<TT>int
</TT> pointers like
56 this. How, then, is is possible to write
58 char *cp = malloc(
10);
59 int *ip = malloc(sizeof(int));
61 without error on either line?
65 </B></p><OL><li>Write a program to read lines and print only those containing
67 (For now, the word can be a constant string in the program.)
69 (which I have to confess I have parroted exactly from K
&R Sec.
4.1)
72 while(
<I>there's another line
</I>)
74 if(
<I>line contains word
</I>)
75 <I>print the line
</I>;
78 Use the
<TT>strstr
</TT> function (mentioned in the notes) to look for the
79 word. Be sure to include the line
81 #include
<string.h
>
83 at the top of the source file where you call
<TT>strstr
</TT>.
84 <li>Rewrite the checkbook-balancing program from
85 assignment
4 (exercise
6)
87 <TT>getwords
</TT> function (from the notes) to make it easy to take the
88 word ``check'' or ``deposit'',
<I>and
</I> the amount, from a single line.
89 <li>Rewrite the line-reversing function from
90 assignment
4 (exercise
9)
92 <li>Rewrite the character-counting function from
93 assignment
5 (exercise
3)
95 <li>Rewrite the string-concatenation program from assignment
5 (exercise
4)
96 to call
<TT>malloc
</TT> to allocate a new piece of memory just big enough
97 for the concatenated result. Don't forget to leave room for the
99 <li>Rewrite the string-replacing function from
100 assignment
5 (exercise
5)
103 Write a program to read lines of text up to
<TT>EOF
</TT>, and
104 then print them out in reverse order. You can use
<TT>getline
</TT> to
105 read each line into a fixed-size array (as we have been doing
106 all along), but you will have to call
<TT>malloc
</TT> and make a copy of
107 each line before you read the next one. Also, you will have to
108 use
<TT>malloc
</TT> and
<TT>realloc
</TT> to maintain the ``array'' of character pointers
109 which holds all of the lines. (Your code will be similar to that
110 in section
11.3 of the notes, p.
5)
113 Extra credit: remove the restriction imposed by the fixed-size
114 array into which each line is originally read; allow the program
115 to accept arbitrarily many arbitrarily-long lines. (You'll have
116 to replace
<TT>getline
</TT> with a dynamically-allocating line-getting
117 function which calls
<TT>malloc
</TT> and
<TT>realloc
</TT>.)
121 This page by
<a href=
"http://www.eskimo.com/~scs/">Steve Summit
</a>
122 //
<a href=
"copyright.html">Copyright
</a> 1995-
9
123 //
<a href=
"mailto:scs@eskimo.com">mail feedback
</a>