K&R C Programs Exercise 4-8

Exercise 4-8. Suppose that there will never be more than one character of pushback. Modify getch and ungetch accordingly. The original K&R implementation uses an array buf[BUFSIZE] and an index bufp to support multiple pushed-back characters. If only one character of pushback is ever needed, the array becomes unnecessary. A single static int buf suffices …

K&R C Programs Exercise 4-5

Exercise 4-5. Add access to library functions like sin, cos, exp, and pow. See <math.h> in Appendix B, Section 4. Math functions need the calculator to recognize words as well as single characters. getop currently returns one character at a time. The fix: if getop sees a letter, it reads the whole word into s[] …

C Program to add one to digits of a number

Write C Program to add one to digits of a number. C Program that adds the 1 to each single digit of a number, i.e for Example 12345’s output is 23456. If the digit is 9 it adds 1 and follows the carry system, 9 becomes 0 and 9’s left digit adds one more 1. …

K&R C Programs Exercise 4-4

Exercise 4-4. Add commands to print the top element of the stack without popping it, to duplicate it, and to swap the top two elements. Add a command to clear the stack. Four new commands, each operating directly on the stack array. None requires new data structures — just index arithmetic on val[] and sp. …

K&R C Programs Exercise 4-2

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. The K&R atof in Section 4.2 handles sign, integer part, and fractional part. Scientific notation adds a third stage: after the mantissa, look for e or E, …

K&R C Programs Exercise 4-1

Exercise 4-1. Write the function strindex(s,t) that returns the position of the rightmost occurrence of t in s, or -1 if there is none. The K&R strindex in Section 4.1 returns the leftmost match by scanning left-to-right and returning at the first hit. For the rightmost match there are two symmetric approaches: scan right-to-left and …