We have already provided solutions to all the exercises in the book “C 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.

 Exercise 41. 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 41.
 Exercise 41. Write the function strrindex(s,t) , which returns the position of the rightmost occurrence of t in s , or 1 if there is none.

 Exercise 42.Extend atof to handle scientific notation of the form 123.45e6 where a floatingpoint number may be followed by e or E and an optionally signed exponent.
Solution to Exercise 42.
 Exercise 42.Extend atof to handle scientific notation of the form 123.45e6 where a floatingpoint number may be followed by e or E and an optionally signed exponent.

 Exercise 43.Given the basic framework, it’s straightforward to extend the calculator. Add the modulus ( % ) operator and provisions for negative numbers.
Solution to Exercise 43.
 Exercise 43.Given the basic framework, it’s straightforward to extend the calculator. Add the modulus ( % ) operator and provisions for negative numbers.

 Exercise 44. 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 44.
 Exercise 44. 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.

 Exercise 45. Add access to library functions like sin , exp , and pow . See <math.h> in Appendix B, Section 4.
Solution to Exercise 45.
 Exercise 45. Add access to library functions like sin , exp , and pow . See <math.h> in Appendix B, Section 4.

 Exercise 46. Add commands for handling variables. (It’s easy to provide twentysix variables with singleletter names.) Add a variable for the most recently printed value.
Solution to Exercise 46.  Exercise 47. 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 47.
 Exercise 46. Add commands for handling variables. (It’s easy to provide twentysix variables with singleletter names.) Add a variable for the most recently printed value.

 Exercise 48.Suppose that there will never be more than one character of pushback. Modify getch and ungetch accordingly.
Solution to Exercise 48.
 Exercise 48.Suppose that there will never be more than one character of pushback. Modify getch and ungetch accordingly.

 Exercise 49.Our getch and ungetch do not handle a pushedback EOF correctly. Decide what their properties ought to be if an EOF is pushed back, then implement your design
Solution to Exercise 49.
 Exercise 49.Our getch and ungetch do not handle a pushedback EOF correctly. Decide what their properties ought to be if an EOF is pushed back, then implement your design

 Exercise 410. 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 410.
 Exercise 410. An alternate organization uses getline to read an entire input line; this makes getch and ungetch unnecessary. Revise the calculator to use this approach.

 Exercise 411. Modify getop so that it doesn’t need to use ungetch. Hint: use an internal static variable.
Solution to Exercise 411.
 Exercise 411. Modify getop so that it doesn’t need to use ungetch. Hint: use an internal static variable.

 Exercise 412. 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 412.
 Exercise 412. 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.

 Exercise 413. Write a recursive version of the function reverse(s) , which reverses the string s in place.
Solution to Exercise 413.
 Exercise 413. Write a recursive version of the function reverse(s) , which reverses the string s in place.
 Exercise 414. Define a macro swap(t,x,y) that interchanges two arguments of type t . (Block structure will help.)
Solution to Exercise 414.
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!