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 4-13

Exercise 4-13. Write a recursive version of the function reverse(s), which reverses the string s in place. An iterative reverse uses two indices walking inward from both ends, swapping until they meet. The recursive version does the same with the call stack: each call receives left and right indices, swaps those two characters, then recurses …

K&R C Programs Exercise 4-7

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? The answer is: ungets should use ungetch, not access buf and bufp directly. This is the principle of information hiding — getch and ungetch own the …

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 …

K\&R C Exercise 2-5: any — Find First Matching Character

K and R C, Solution to Exercise 2-5: K and R C Programs Exercises provides the solution to all the exercises in the C Programming Language, second addition, by Brian W.Keringhan and Dennis M.Ritchie(Prentice Hall,1988). You can learn and solve K&R C Programs Exercise.Write the C function any(s1, s2), which returns the first location in the …

K&R C Exercise 2-4: squeeze — Delete Characters from a String

Exercise 2-4. Write an alternative version of squeeze(s1,s2) that deletes each character in s1 that matches any character in the string s2. Approach The original K&R squeeze(s, c) removes all occurrences of a single character c from string s using a compact two-index pattern: index i scans every position in s, while index j only …