This document provides practice questions and exercises for chapters 4 and 5 of a programming in C course. Chapter 4 asks students to modify string programs to take input from the user and check for string equality. It also asks students to write a program to condense code by replacing whitespace with numbers. Chapter 5 asks students to modify an existing data analysis program to display output in different formats based on user input and include help. It describes separate functions stored in a file called func.c that contain the calculation code.
1. Programming in C
Objectives
In this session, you will do the practice questions of Chapter
4 and Chapter 5.
Ver. 1.0 Slide 1 of 7
2. Programming in C
Chapter 4
1. Modify the program that determines the length of a string
so that it allows input of the string from the user and
displays its length. (Maximum size of the input string is 80).
2. Modify the string comparison program so that it allows
input of strings and reports on whether they are the same
or not.
Ver. 1.0 Slide 2 of 7
3. Programming in C
Chapter 4 (Contd.)
3. A program can be easily understood if it is indented
properly. This has one drawback, in terms of the size of the
program on the disk. One simple way of cutting down the
size significantly is described here.
Assume the following piece of code:
123456789012345678901234567890 (indicates column)
printf("This is the last straw");
The 15 blanks at the start of the line can be replaced by the
number 15 so that the line of code is now:
123456789012345678901234567890 (indicates column)
15printf("This is the last straw");
Note that a saving of 13 bytes (15 - 2) has been
accomplished at one shot.
Ver. 1.0 Slide 3 of 7
4. Programming in C
Chapter 4 (Contd.)
Write a program, which takes in a line of code from the user
(maximum length of 80 characters) and prints out the
condensed string (with the number of spaces occupying the
first two characters in the condensed string).
Ver. 1.0 Slide 4 of 7
5. Programming in C
Chapter 5
1. Alcatel Automatics is a company known for its marketing
success. This success has been largely due to its superb
data analysis programs.
The product manager wants some modifications to the
existing programs. When running the program, he should be
able to specify any of the following with the run command
itself:
%s: for displaying the product sales by each salesman as a
percentage of the total salesman sales.
%p: for displaying the product sales by each salesman as a
percentage of the total product sales.
%i: for displaying the product sales as an index of total sales of
all products
Ver. 1.0 Slide 5 of 7
6. Programming in C
Chapter 5 (Contd.)
He should also be shown some help message to assist him
in case he forgets what to specify with the command and
should then be able to give the command again.
Since the calculations will be coded in separate functions,
which are stored together in one file called func.c,
separate from the main() function. The function available
in this file are:
intdat(): Displays the data as an index of total sales.
proddata(): Displays the data as a percentage of total
product sales.
calcprodtot(): Calculates the salesman-wise totals.
Ver. 1.0 Slide 6 of 7
7. Programming in C
Chapter 5 (Contd.)
The contents of func.c are as follows:
Microsoft Word
Document
Ver. 1.0 Slide 7 of 7
Editor's Notes
Begin the session by explaining the objectives of the session.