Exercise: Modify Program P6.12 to send the output to a file, linecopy.txt.
Exercise: Write a program to copy the contents of a file, input.txt, to a file, copy.txt.
Hint: you just need to make minor changes to Program P6.10.
The sequence of characters 385 is converted as follows:
num = 0
get '3'; convert to 3
num = num*10 + 3 = 0*10 + 3; num is now 3 get '8'; convert to 8
num = num*10 + 8 = 3*10 + 8; num is now 38 get '5'; convert to 5
num = num*10 + 5 = 38*10 + 5; num is now 385 There are no more digits and the final value of num is 385.
Let us use this idea to write a program that reads data character by character until it finds an integer. It constructs and prints the integer.
The program will have to read characters until it finds a digit, the first of the integer. Having found the first digit, it must construct the integer by reading characters as long as it keeps getting a digit. For example, suppose the data was this:
Number of items: 385, all in good condition
The program will read characters until it finds the first digit, 3. It will construct the integer using the 3 and then reading 8 and 5. When it reads the comma, it knows the integer has ended.
This outline can be expressed in pseudocode as follows:
read a character, ch while ch is not a digit do read a character, ch endwhile
//at this point, ch contains a digit while ch is a digit do
use ch to build the integer read a character, ch
endwhile
print the integer
How do we test if the character in ch is a digit? We must test if ch >= '0' && ch <= '9'
If this is true, we know that the character is between '0' and '9', inclusive. Conversely, to test if ch is not a digit, we can test if
ch < '0' || ch > '9'
Putting all these ideas together gives us Program P6.13.
Program P6.13
#include <stdio.h>
int main() {
printf("Type data including a number and press \"Enter\"\n");
char ch = getchar();
// as long as the character is not a digit, keep reading while (ch < '0' || ch > '9') ch = getchar() ;
// at this point, ch contains the first digit of the number int num = 0;
while (ch >= '0' && ch <= '9') { // as long as we get a digit num = num * 10 + ch - '0'; // update num
ch = getchar();
}
printf("Number is %d\n", num);
}
A sample run is shown below:
Type data including a number and press "Enter"
hide the number &(%%)4719&*(&^ here Number is 4719
This program will find the number, no matter where it is hidden in the line.
eXerCISeS 6
1. Give the range of asCII codes for (a) the digits (b) the uppercase letters (c) the lowercase letters.
2. how is the single quote represented as a character constant?
3. What is the character value of a character constant?
4. What is the numeric value of a character constant?
5. how is the expression 5 + 'T' evaluated? What is its value?
6. What value is assigned to n by n = 7 + 't'?
7. What character is stored in ch by ch = 4 + 'n'?
8. If ch = '8', what value is assigned to d by d = ch - '0'?
9. If ch contains any uppercase letter, explain how to change ch to the equivalent lowercase letter.
10. If ch contains any lowercase letter, explain how to change ch to the equivalent uppercase letter.
11. Write a program to request a line of data and print the first digit on the line.
12. Write a program to request a line of data and print the first letter on the line.
13. Write a program to request a line of data and print the number of digits and letters on the line.
14. Write a program to read a passage from a file and print how many times each vowel appears.
15. Modify program p6.13 so that it will find negative integers as well.
16. Write a program that reads a file containing a C program and outputs the program to another file with all the // comments removed.
17. Write a program to read the data, character by character, and store the next number (with or without a decimal point) in a double variable (dv, say). For example, given the following data your program should store 43.75 in dv.
Mary works for $43.75 per hour
18. In the programming language pascal, comments can be enclosed by { and } or by (* and *). Write a program which reads a data file input.pas containing pascal code and writes the code to a file output.pas, replacing each { with (* and each } with *). For example, the statements
read(ch); {get the first character}
while ch = ' ' do {as long as ch is a blank}
read(ch); {get another character}
writeln('The first non-blank is ', ch);
should be converted to
read(ch); (*get the first character*)
while ch = ' ' do (*as long as ch is a blank*) read(ch); (*get another character*)
writeln('The first non-blank is ', ch);
19. You are given the same data as in 17, but now remove the comments altogether.
20. someone has typed a letter in a file letter.txt, but does not always start the word after a period with a capital letter. Write a program to copy the file to another file format.txt so that all words after a period now begin with a capital letter.
also ensure there is exactly one space after each period. For example, the text Things are fine. we can see you now. let us know when is a good time. bye for now.
must be rewritten as
Things are fine. We can see you now. Let us know when is a good time. Bye for now.
Functions
In this chapter, we will explain the following:
• Why functions are important in programming
• How to write functions
• What happens when a function is called
• Where functions are placed in a program
• Some important concepts relating to functions using several examples
7.1 About Functions
So far, all our programs have consisted of a single function called main. However, we have made use of predefined C functions such as printf, scanf, strcpy, and fopen. When we run a program, it starts executing with the first statement in main and ends when it reaches the last statement.
As we have seen, it is possible to write reasonably useful programs with only main. However, there are many limitations to this approach. The problem to be solved may be too complex to be solved with one function. We may need to break it up into subproblems and try to solve each of these individually. It would be impractical to solve all the subproblems in one function. It might be better to write a separate function to solve each subproblem.
Also, we may want to reuse the solution to common problems. It would be difficult to reuse a solution if it is part of the solution to a bigger problem. For example, if we need the highest common factor (HCF) of two numbers in several places, it would be best to write a routine that works out the HCF of two given numbers; we call this routine whenever we need to find the HCF of two numbers.
A well-written function performs some well-defined task; for example, skip a specified number of lines in the output or arrange some numbers in ascending order. However, quite often, a function also returns a value; for example, calculate the salary of a person and return the answer or play one turn of a game and return the score for that turn. The value returned is normally used at the point from which the function was called.
Previously, we used the string function strcmp, which returns a value that tells us the result of comparing two strings. And we have used getchar and getc to return the next character in the input.
We are now ready to learn how to write our own functions (called user-defined functions),