html>C-ohjelmointi, laskuharj. 1

C-programming Fall 1999

Problems for Exercise 4 (11.-15.10.1999)

Write your C-programs from the very beginning with a computer. Points will be given only for solutions proven by a computer. Make sure of the correct functioning by compiling your program, correcting possible errors and by running program, if necessary using different test inputs. Avoid using, in your solutions, such features that have not been explained in the course.

1. Define a macro SWAP(T,X,Y) that interchanges two arguments, X and Y, of type T. (The C block structure and combined statements will help.)

2. Write a function find(char *s, char c) that returns the pointer to the first occurrence of character c in the given or NULL if character c is not found. Do not use the standard library functions (except for input and output). Use pointers to access strings.

3. Write a function insert(char *result, char *to, char *from, char c) that inserts the string from into the string to after the first occurrence of the charcter c in to. The result is given in string result. If c does not occur in string to, the string to is just copied into string result. Use pointers to access strings.

4. Write a function expand(char *s1, char *s2) that expands shorthand notations like a-d or 1-6 into the equivalent complete list abcd or 123456. The shorthand notations of string s1 should be expanded and saves into string s2. Allow for letters of either case and digits.

5. Write a function that counts from a string the amount of either small or capital letters. The size of the letters to be counted is given as a parameter.

6. A student A has written a text file by using only upper case letters. She/he wants to make her/his file better so that each sentence starts with an upper case letter and otherwise, the file contains only lower case letters. Write a program that copies characters from standard input to standard output so that the upper case letters are changed to lower case letters, but the first letter of each sentence remains in the upper case. The beginning of each sentence can be recognized by looking for letters that are preceeded by full stop, exclamation mark or query and one or more blanks or newlines after that. (Your program does not need to recognize more complicated cases.) Your program does not have to take into account scandinavian letters (å,ä, ö).