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 #
5</title>
12 <H1>Assignment #
5</H1>
18 <B>Introductory C Programming
21 UW Experimental College
26 </B></p><p><a href=
"PS5.html">Assignment #
5</a>
27 <br><a href=
"PS4a.html">Answers to Assignment #
4</a>
28 <br><a href=
"http://www.eskimo.com/~scs/cclass/notes/sx9.html">Class Notes, Chapter
9</a>
29 <br><a href=
"http://www.eskimo.com/~scs/cclass/notes/sx10.html">Class Notes, Chapter
10</a>
30 <p><B>Reading Assignment:
31 </B></p><p><a href=
"http://www.eskimo.com/~scs/cclass/notes/sx9.html">Class Notes, Chapter
9</a>
32 <p><B>Review Questions:
33 </B></p><OL><li>What's wrong with this
<TT>#define
</TT> line?
37 <li>Suppose you defined the macro
41 Then, suppose you used it in another expression:
45 What value would
<TT>x
</TT> be set to?
46 <li>If the header file
<TT>x.h
</TT> contains an external prototype declaration
47 for a function
<TT>q()
</TT>,
48 where should
<TT>x.h
</TT> be included?
50 How many differences can you think of between
<TT>i
</TT> and
51 <TT>J
</TT> as defined by
57 (Hint: think about trying to write
<TT>J =
5</TT>, or
<TT>int a[i]
</TT>.)
61 </B></p><OL><li>Pick one of the programs we've worked with so far
62 that uses the
<TT>getline
</TT> function,
63 such as the
<TT>getline
</TT> test program
65 (assignment
4, exercise
2)
66 or the ``word zipping'' program
68 (assignment
4, tutorial
3)
69 or the checkbook balancing program
71 (assignment
4, exercise
6).
73 Arrange the program in two source files,
74 one containing
<TT>main()
</TT>,
75 and one containing
<TT>getline()
</TT>.
76 If possible, set your compiler to warn you
77 when functions are called or defined without a prototype in scope.
78 Create a header file
<TT>getline.h
</TT>
79 containing the external function prototype for
<TT>getline()
</TT>,
80 and
<TT>#include
</TT> it as you see fit.
81 Also use a preprocessor macro such as
<TT>MAXLINE
</TT>
82 for the maximum line length
83 (i.e. in declarations of
<TT>line
</TT> arrays,
84 and calls to
<TT>getline
</TT>).
85 <li>Write a program to read its input and write it out, double-spaced
86 (that is, with an extra blank line between each line).
87 Process the input a character at a time;
89 entire lines at a time
90 using
<TT>getline
</TT>.
91 <li>Write the function
93 int countchars(char string[], int ch);
95 which returns the number of times the character
<TT>ch
</TT> appears in the
99 countchars(
"Hello, world!", 'o')
102 <li>Write a short program to read two lines of text, and concatenate
103 them using
<TT>strcat
</TT>. Since
<TT>strcat
</TT> concatenates in-place, you'll
104 have to make sure you have enough memory to hold the concatenated
105 copy. For now, use a
<TT>char
</TT> array which is twice as big as either
106 of the arrays you use for reading the two lines. Use
<TT>strcpy
</TT> to
107 copy the first string to the destination array, and
<TT>strcat
</TT> to
108 append the second one.
109 <li>Write the function
111 replace(char string[], char from[], char to[])
113 which finds the string
<TT>from
</TT> in the string
<TT>string
</TT>
116 it with the string
<TT>to
</TT>.
117 You may assume that
<TT>from
</TT>
118 and
<TT>to
</TT> are the same length. For example, the code
120 char string[] =
"recieve";
121 replace(string,
"ie",
"ei");
123 should change
<TT>string
</TT> to
<TT>"receive"</TT>.
127 Think about what
<TT>replace()
</TT> should do
128 if the
<TT>from
</TT> string appears multiple times
129 in the input string.)
133 This page by
<a href=
"http://www.eskimo.com/~scs/">Steve Summit
</a>
134 //
<a href=
"copyright.html">Copyright
</a> 1995-
9
135 //
<a href=
"mailto:scs@eskimo.com">mail feedback
</a>