Tutorial Exercises Weeks 7 and 8

  1. Write a C program which reads characters from standard input and writes to standard output. It converts the characters read in the following way: all upper case characters to the equivalent lower case, all lower case to the equivalent upper case, and all others left unaltered.
  2. Write a program that reads characters from standard input, checking that they are all digits. The program should exit with a zero exit status if it reaches end-of-line and all characters read are digits. If it reads a non-digit character it should cease reading and exit with a non-zero exit status.
  3. Look in the files /usr/include/limits.h. What are the largest and smallest integers that are supported? Write and test a program to print out their values.
  4. The following program has a common and very serious error. What is it? What happens when you attempt to run it? int main(int argc, char *argv[]) { char *p; *p = 'a'; }
  5. Write a program using an array to read in a set of numbers terminated by EOF and print them out in reverse order. Assume that no more than 100 numbers need to be read.
  6. Write a function to emulate strcmp to compare two null-terminated strings. It should return -1 if the first string is lexicographically less than the second, 1 if it is greater, and 0 if they are the same.
  7. Write a program to store an array of records containing student names and IDs. The program should read upto 100 records. It should then enter a loop reading student IDs and printing the corrresponding name if found.
  8. Write a program to read in strings (one per line), store pointers to them in an array and print them out. NB:You will need to use ``malloc'' to create heap space to store copies of the strings.
  9. Use the online manual to read the entry in section three for ``getwd''. Write a program to print your current working directory. Note the use of MAXPATHLEN - no complete pathname is allowed to be longer than that.
  10. Use the online manual to read the entry in section two for ``chdir''. Write a program to read directory names from standard input until end-of-file and change the current working directory. What patterns are recognised out of `.', `..', `~', etc?
  11. Use the online manual to read the entry in section two for "stat". Write a program that will take one command line argument and write the file permissions mode (mode_t) to stdout as an octal number.
email: jan@newmarch.name
Web: http://jan.newmarch.name/
Last modified: 10 April 2001