K&R C Programs Exercise 5-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. Advance both pointers to their respective null terminators, then walk backwards together comparing characters. If t runs out first (pointer reaches the start of t), every character matched — t is …

K&R C Programs Exercise 5-3

Exercise 5-3. Write 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. The Chapter 2 version used array indexing with integer subscripts. The pointer version eliminates the index variables entirely: advance s to its null terminator, then copy t character-by-character (including …

K&R C Programs Exercise 5-1

Exercise 5-1. As written, getint treats a + or – not followed by a digit as a valid representation of zero. Fix getint so that it pushes such a character back on the input. K&R’s getint from Section 5.2 reads an optional sign then digits. The bug: if + or – appears but the next …

K&R C Programs Exercise 4-11

Exercise 4-11. Modify getop so that it doesn’t need to use ungetch. Hint: use an internal static variable. In the original calculator, getop calls ungetch once: when it reads a non-digit character to terminate a number, it pushes that character back so the next call to getop will see it. The fix: instead of pushing …

C Program to find sum and product using macros.

Write C Program to find sum and product using macros.Macro is a piece of text that is expanded by the preprocessor part of the compiler. This is used in to expand text before compiling. Macro definitions (#define), and conditional inclusion (#if). In many C implementations, it is a separate program invoked by the compiler as …

K&R C Programs Exercise 4-10

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. The original calculator reads the input one character at a time via getch/ungetch. This exercise replaces that machinery with a line-buffer approach: read the whole line into a static …