* better
[mascara-docs.git] / lang / C / the.ansi.c.programming.language / c.programming.notes / homework / PS5.html
blobd487013901dde23b714e5d5ace9d639b5f87eb6c
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. -->
5 <html>
6 <head>
7 <link rev="owner" href="mailto:scs@eskimo.com">
8 <link rev="made" href="mailto:scs@eskimo.com">
9 <title>Assignment #5</title>
10 </head>
11 <body>
12 <H1>Assignment #5</H1>
18 <B>Introductory C Programming
19 <br>
20 <br>
21 UW Experimental College
22 </B><br>
23 <br>
24 <B>Assignment #5
25 </B><p><B>Handouts:
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?
34 <pre>
35 #define N 10;
36 </pre>
37 <li>Suppose you defined the macro
38 <pre>
39 #define SIX 2*3
40 </pre>
41 Then, suppose you used it in another expression:
42 <pre>
43 int x = 12 / SIX;
44 </pre>
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?
49 <li>(harder)
50 How many differences can you think of between <TT>i</TT> and
51 <TT>J</TT> as defined by
52 these two lines?
53 <pre>
54 int i = 10;
55 #define J 10
56 </pre>
57 (Hint: think about trying to write <TT>J = 5</TT>, or <TT>int a[i]</TT>.)
58 </OL><br>
59 <br>
60 <p><B>Exercises:
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;
88 do <em>not</em> read
89 entire lines at a time
90 using <TT>getline</TT>.
91 <li>Write the function
92 <pre>
93 int countchars(char string[], int ch);
94 </pre>
95 which returns the number of times the character <TT>ch</TT> appears in the
96 <TT>string</TT>.
97 For example, the call
98 <pre>
99 countchars("Hello, world!", 'o')
100 </pre>
101 would return 2.
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
110 <pre>
111 replace(char string[], char from[], char to[])
112 </pre>
113 which finds the string <TT>from</TT> in the string <TT>string</TT>
115 and replaces
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
119 <pre>
120 char string[] = "recieve";
121 replace(string, "ie", "ei");
122 </pre>
123 should change <TT>string</TT> to <TT>"receive"</TT>.
124 <br>
125 <br>
126 (Extra credit:
127 Think about what <TT>replace()</TT> should do
128 if the <TT>from</TT> string appears multiple times
129 in the input string.)
130 </OL><hr>
131 <hr>
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>
136 </p>
137 </body>
138 </html>