K&R C Programs Exercise 7-7

Exercise 7-7. Modify the pattern finding program of Chapter 5 to take its input from a set of named files or, if no files are named, from the standard input. Should the file name be printed when a match is found? Yes — when searching multiple files the filename should prefix each matching line (the …

K&R C Programs Exercise 7-6

Exercise 7-6. Write a program to compare two files, printing the first line where they differ. Open both files, read them line by line in parallel, and compare. When a differing line is found, print both versions with a line number and exit. Also handle the case where one file is longer than the other. …

K&R C Programs Exercise 7-5

Exercise 7-5. Rewrite the postfix calculator of Chapter 4 to use scanf and/or sscanf to do the input and number conversion. The original Chapter 4 calculator used a custom getop() that read characters one at a time with getch/ungetch to recognise numbers vs operators. With scanf we replace that with a simpler approach: read one …

K&R C Programs Exercise 7-4

Exercise 7-4. Write a private version of scanf analogous to the private printf described in this section. Like minprintf, minscanf parses a format string and uses va_list to accept a variable number of pointer arguments. It reads from stdin and dispatches to the appropriate conversion — using scanf itself with a constructed sub-format string, so …

K&R C Programs Exercise 7-3

Exercise 7-3. Revise minprintf to handle more of the other facilities of printf. K&R’s minprintf (Section 7.3) handles only %d and %s. The cleanest way to extend it: as we parse the format string, collect the complete format specifier (flags, width, precision, length modifier, conversion character) into a local buffer, then call printf with that …

K&R C Programs Exercise 7-1

Exercise 7-1. Write a program that converts upper case to lower or lower case to upper, depending on the name it is invoked with, as found in argv[0]. Unix convention: a single binary is installed under two names via symlinks (tolower and toupper). argv[0] holds the name used to invoke the program. Checking whether “lower” …