K & R C Exercise Solutions.

We have already provided solutions to all the exercises in the bookC Programming Language (2nd Edition) popularly known as K & R C book.

In this blog post I will give links to all the chapters of the “C Programming Language (2nd Edition)” popularly known as K & R C book for easy reference.

  1. Chapter 1: A Tutorial Introduction
  2. Chapter 2: Types, Operators and Expressions
  3. Chapter 3: Control Flow
  4. Chapter 4: Functions and Program Structure
  5. Chapter 5: Pointers and Arrays
  6. Chapter 6: Structures
  7. Chapter 7: Input and Output
You can purchase the book from here or here.

To get regular updates on new C programs, you can Follow @c_program

You can discuss these programs on our Facebook Page. Start a discussion right now,

our page!

Like to get updates right inside your feed reader? Grab our feed!

To browse more C Programs visit this link
(c) www.c-program-example.com

K & R C Chapter 5 Exercise Solutions.

We have already provided solutions to all the exercises in the bookC Programming Language (2nd Edition) popularly known as K & R C book.

In this blog post I will give links to all the exercises from Chapter 5 of the book for easy reference.

Chapter 5: Pointers and Arrays

  1. Exercise 5-1. As written, getint treats a + or – not followed by a digit as a valid representation of zero. Fix it to push such a character back on the input.
    Solution to Exercise 5-1.
  2. Exercise 5-2.Write getfloat , the floating-point analog of getint . What type does getfloat return as its function value?
    Solution to Exercise 5-2.
  3. Exercise 5-3.GWrite a pointer version of the function strcat that we showed in Chapter 2: strcat(s,t) copies the string t to the end of s .
    Solution to Exercise 5-3.
  4. Exercise 5-4. Write the function strend(s,t) , which returns 1 if the string t occurs at the end of the string s , and zero otherwise.
    Solution to Exercise 5-4.
  5. Exercise 5-5. Write versions of the library functions strncpy , strncat , and strncmp , which operate on at most the first n characters of their argument strings. For example, strncpy(s,t,n) copies at most n characters of t to s . Full descriptions are in Appendix B.
    Solution to Exercise 5-5.
  6. Exercise 5-6. Rewrite appropriate programs from earlier chapters and exercises with pointers instead of array indexing. Good possibilities include getline (Chapters 1 and 4), atoi , itoa , and their variants (Chapters 2, 3, and 4), reverse (Chapter 3), and strindex and getop (Chapter 4).
    Solution to Exercise 5-6.
  7. Exercise 5-7. Rewrite readlines to store lines in an array supplied by main , rather than calling alloc to maintain storage. How much faster is the program?
    Solution to Exercise 5-7.
  8. Exercise 5-8.There is no error-checking in day_of_year or month_day. Remedy this defect.
    Solution to Exercise 5-8.
  9. Exercise 5-9.Rewrite the routines day_of_year and month_day with pointers instead of indexing.
    Solution to Exercise 5-9.
  10. Exercise 5-10. Write the program expr , which evaluates a reverse Polish expression from the command line, where each operator or operand is a separate argument. For example, expr 2 3 4 + * evaluates 2 X (3+4).
    Solution to Exercise 5-10.
  11. Exercise 5-11. Modify the programs entab and detab (written as exercises in Chapter 1) to accept a list of tab stops as arguments. Use the default tab settings if there are no arguments.
    Solution to Exercise 5-11.
  12. Exercise 5-12. Extend entab and detab to accept the shorthand entab -m +n to mean tab stops every n columns, starting at column m . Choose convenient (for the user) default behavior.
    Solution to Exercise 5-12.
  13. Exercise 5-13. Write the program tail, which prints the last n lines of its input. By default, n is 10, say, but it can be changed by an optional argument, so that tail -n prints the last n lines. The program should behave rationally no matter how unreasonable the input or the value of n. Write the program so it makes the best use of available storage; lines should be stored as in the sorting program of Section 5.6, not in a two-dimensional array of fixed size.
    Solution to Exercise 5-13.
  14. Exercise 5-14. Modify the sort program to handle a -r flag, which indicates sorting in reverse (decreasing) order. Be sure that -r works with -n.
    Solution to Exercise 5-14.
  15. Exercise 5-15.Add the option -f to fold upper and lower case together, so that case distinctions are not made during sorting; for example, a and A compare equal.
    Solution to Exercise 5-15.
  16. Exercise 5-16.Add the -d (“directory order”) option, which makes comparisons only on letters, numbers and blanks. Make sure it works in conjunction with -f .
    Solution to Exercise 5-16.
  17. Exercise 5-17. Add a field-handling capability, so sorting may be done on fields within lines, each field sorted according to an independent set of options. (The index for this book was sorted with -df for the index category and -n for the page numbers.)
    Solution to Exercise 5-10.
  18. Exercise 5-18. Make dcl recover from input errors.
    Solution to Exercise 5-18.
  19. Exercise 5-19. Modify undcl so that it does not add redundant parentheses to declarations.
    Solution to Exercise 5-19.
  20. Exercise 5-20. Expand dcl to handle declarations with function argument types, qualifiers like const , and so on.
    Solution to Exercise 5-20.
You can purchase the book from here or here.

To get regular updates on new C programs, you can Follow @c_program

You can discuss these programs on our Facebook Page. Start a discussion right now,

our page!

Like to get updates right inside your feed reader? Grab our feed!

To browse more C Programs visit this link
(c) www.c-program-example.com

K & R C Chapter 4 Exercise Solutions.

We have already provided solutions to all the exercises in the bookC Programming Language (2nd Edition) popularly known as K & R C book.

In this blog post I will give links to all the exercises from Chapter 4 of the book for easy reference.

Chapter 4: Functions and Program Structure
    1. Exercise 4-1. Write the function strrindex(s,t) , which returns the position of the rightmost occurrence of t in s , or -1 if there is none.
      Solution to Exercise 4-1.
    1. Exercise 4-2.Extend atof to handle scientific notation of the form 123.45e-6 where a floating-point number may be followed by e or E and an optionally signed exponent.
      Solution to Exercise 4-2.
    1. Exercise 4-3.Given the basic framework, it’s straightforward to extend the calculator. Add the modulus ( % ) operator and provisions for negative numbers.
      Solution to Exercise 4-3.
    1. Exercise 4-4. Add commands to print the top element of the stack without popping, to duplicate it, and to swap the top two elements. Add a command to clear the stack.
      Solution to Exercise 4-4.
    1. Exercise 4-5. Add access to library functions like sin , exp , and pow . See <math.h> in Appendix B, Section 4.
      Solution to Exercise 4-5.
    1. Exercise 4-6. Add commands for handling variables. (It’s easy to provide twenty-six variables with single-letter names.) Add a variable for the most recently printed value.
      Solution to Exercise 4-6.
    2. Exercise 4-7. Write a routine ungets(s) that will push back an entire string onto the input. Should ungets know about buf and bufp , or should it just use ungetch ?
      Solution to Exercise 4-7.
    1. Exercise 4-8.Suppose that there will never be more than one character of pushback. Modify getch and ungetch accordingly.
      Solution to Exercise 4-8.
    1. Exercise 4-9.Our getch and ungetch do not handle a pushed-back EOF correctly. Decide what their properties ought to be if an EOF is pushed back, then implement your design
      Solution to Exercise 4-9.
    1. Exercise 4-10. An alternate organization uses getline to read an entire input line; this makes getch and ungetch unnecessary. Revise the calculator to use this approach.
      Solution to Exercise 4-10.
    1. Exercise 4-11. Modify getop so that it doesn’t need to use ungetch. Hint: use an internal static variable.
      Solution to Exercise 4-11.
    1. Exercise 4-12. Adapt the ideas of printd to write a recursive version of itoa ; that is, convert an integer into a string by calling a recursive routine.
      Solution to Exercise 4-12.
    1. Exercise 4-13. Write a recursive version of the function reverse(s) , which reverses the string s in place.
      Solution to Exercise 4-13.
  1. Exercise 4-14. Define a macro swap(t,x,y) that interchanges two arguments of type t . (Block structure will help.)
    Solution to Exercise 4-14.
You can purchase the book from here or here.

To get regular updates on new C programs, you can Follow @c_program

You can discuss these programs on our Facebook Page. Start a discussion right now,

our page!

Like to get updates right inside your feed reader? Grab our feed!

To browse more C Programs visit this link

(c) www.c-program-example.com

K & R C Chapter 2 Exercise Solutions

We have already provided solutions to all the exercises in the bookC Programming Language (2nd Edition) popularly known as K & R C book.

In this blog post I will give links to all the exercises from Chapter 2 of the book for easy reference.

Chapter 2: Types, Operators and Expressions

  1. Exercise 2-1.Write a program to determine the ranges of char , short , int , and long variables, both signed and unsigned , by printing appropriate values from standard headers and by direct computation. Harder if you compute them: determine the ranges of the various floating-point types.
    Solution to Exercise 2-1.
  2. Exercise 2-2.Write a loop equivalent to the for loop above without using && or || .
    Solution to Exercise 2-2.
  3. Exercise 2-3.Write the function htoi(s) , which converts a string of hexadecimal digits (including an optional 0x or 0X) into its equivalent integer value. The allowable digits are 0 through 9, a through f, and A through F .
    Solution to Exercise 2-3.
  4. Exercise 2-4.Write an alternate version of squeeze(s1,s2) that deletes each character in the string s1 that matches any character in the string s2 .
    Solution to Exercise 2-4.
  5. Exercise 2-5.Write the function any(s1,s2) , which returns the first location in the string s1 where any character from the string s2 occurs, or -1 if s1 contains no characters from s2 . (The standard library function strpbrk does the same job but returns a pointer to the location.)
    Solution to Exercise 2-5.
  6. Exercise 2-6.Write a function setbits(x,p,n,y) that returns x with the n bits that begin at position p set to the rightmost n bits of y, leaving the other bits unchanged.
    Solution to Exercise 2-6.
  7. Exercise 2-7.Write a function invert(x,p,n) that returns x with the n bits that begin at position p inverted (i.e., 1 changed into 0 and vice versa), leaving the others unchanged.
    Solution to Exercise 2-7.
  8. Exercise 2-8.Write a function rightrot(x,n) that returns the value of the integer x rotated to the right by n bit positions.
    Solution to Exercise 2-8.
  9. Exercise 2-9.In a two’s complement number system, x &= (x-1) deletes the rightmost 1-bit in x . Explain why. Use this observation to write a faster version of bitcount .
    Solution to Exercise 2-9.
  10. Exercise 2-10.Rewrite the function lower, which converts upper case letters to lower case, with a conditional expression instead of if-else .
    Solution to Exercise 2-10.
You can purchase the book from here or here.

To get regular updates on new C programs, you can Follow @c_program

You can discuss these programs on our Facebook Page. Start a discussion right now,

our page!

Like to get updates right inside your feed reader? Grab our feed!

To browse more C Programs visit this link
(c) www.c-program-example.com

K & R C Chapter 1 Exercise Solutions

We have already provided solutions to all the exercises in the bookC Programming Language (2nd Edition) popularly known as K & R C book.

In this blog post I will give links to all the exercises from Chapter 1 of the book for easy reference.

Chapter 1: A Tutorial Introduction

  1. Exercise 1-1. Run the “hello, world” program on your system. Experiment with leaving out parts of the program to see what error messages you get.
    Solution to Exercise 1-1.
  2. Exercise 1-2. Experiment to find what happens when printf’s argument string contains c, where c is some character not listed above.
    Solution to Exercise 1-2.
  3. Exercise 1-3. Modify the temperature conversion program to print a heading above the table.
    Solution to Exercise 1-3.
  4. Exercise 1-4. Write a program to print the corresponding Celsius to Fahrenheit table.
    Solution to Exercise 1-4.
  5. Exercise 1-5. Modify the temperature conversion program to print the table in reverse order, that is, from 300 degrees to 0.
    Solution to Exercise 1-5.
  6. Exercise 1-6. Verify that the expression getchar() != EOF is 0 or 1.
    Solution to Exercise 1-6.
  7. Exercise 1-7. Write a program to print the value of EOF.
    Solution to  Exercise 1-7.
  8. Exercise 1-8.Write a program to count blanks, tabs, and newlines.
    Solution to  Exercise 1-8.
  9. Exercise 1-9.Write a program to copy its input to its output, replacing each string of one or more blanks by a single blank.
    Solution to  Exercise 1-9.
  10. Exercise 1-10. Write a program to copy its input to its output, replacing each tab by t , each backspace by b , and each backslash by \ . This makes tabs and backspaces visible in an unambiguous way.
    Solution to  Exercise 1-10.
  11. Exercise 1-11.How would you test the word count program? What kinds of input are most likely to uncover bugs if there are any?
    Solution to  Exercise 1-11.
  12. Exercise 1-12.Write a program that prints its input one word per line.
    Solution to  Exercise 1-12.
  13. Exercise 1-13.Write a program to print a histogram of the lengths of words in its input. It is easy to draw the histogram with the bars horizontal; a vertical orientation is more challenging.
    Solution to  Exercise 1-13.
  14. Exercise 1-14.Write a program to print a histogram of the frequencies of different characters in its input.
    Solution to  Exercise 1-14.
  15. Exercise 1-15.Rewrite the temperature conversion program of Section 1.2 to use a function for conversion.
    Solution to  Exercise 1-15.
  16. Exercise 1-16.Revise the the main routine of the longest-line program so it will correctly print the length of arbitrarily long input lines, and as much as possible of the text.
    Solution to  Exercise 1-16.
  17. Exercise 1-17. Write a program to print all input lines that are longer than 80 characters.
    Solution to  Exercise 1-17.
  18. Exercise 1-18. Write a program to remove all trailing blanks and tabs from each line of input, and to delete entirely blank lines.
    Solution to  Exercise 1-18.
  19. Exercise 1-19. Write a function reverse(s) that reverses the character string s. Use it to write program that reverse thes its input a line at a time.
    Solution to  Exercise 1-19.
  20. Exercise 1-20. Write a program detab that replaces tabs in the input with the proper number of blanks to space to the next tab stop. Assume a fixed set of tab stops, say every n columns. Should n be a variable or a symbolic parameter?
    Solution to  Exercise 1-20.
  21. Exercise 1-21. Write a program entab that replaces strings of blanks with the minimum number of tabs and blanks to achieve the same spacing. Use the same stops as for detab . When either a tab or a single blank would suffice to reach a tab stop, which should be given preference?
    Solution to  Exercise 1-21.
  22. Exercise 1-22. Write a program to “fold” long input lines into two or more shorter lines after the last non-blank character that occurs before the n -th column of input. Make sure your program does something intelligent with very long lines, and if there are no blanks or tabs before the specified column.
    Solution to  Exercise 1-22.
  23. Exercise 1-23. Write a program to remove all comments from a C program. Don’t forget to handle quoted strings and character constants properly. C comments do not nest.
    Solution to  Exercise 1-23.
  24. Exercise 1-24. Write a program to check a C program for rudimentary syntax errors like unbalanced parentheses, brackets and braces. Don’t forget about quotes, both single and double, escape sequences, and comments. (This program is hard if you do it in full generality.)
    Solution to  Exercise 1-24.
You can purchase the book from here or here.

To get regular updates on new C programs, you can Follow @c_program

You can discuss these programs on our Facebook Page. Start a discussion right now,

our page!

Like to get updates right inside your feed reader? Grab our feed!

To browse more C Programs visit this link

(c) www.c-program-example.com