K&R C Exercise 1-20: detab — Replace Tabs with Spaces

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? Approach The key insight is that a tab does not …

C Program to implement Linear regression algorithm.

Linear Regression  is the predicting the value of one scalar variable(y) using the explanatory another variable(x). Linear regression  is represented by the equation Y = a + bX, where X is the explanatory variable and Y is the scalar variable. The slope of the line is b, and a is the intercept. For linear list …

C Program to Implement Radix Sort — LSD with Counting Sort Explained

Radix Sort is a non-comparison sorting algorithm — it never compares two elements directly. Instead, it sorts by processing individual digits, one digit position at a time from least significant to most significant (LSD approach). This lets it sort integers in O(n) time for a fixed number of digits, breaking the O(n log n) lower …

K&R C Exercise 1-18: Remove Trailing Blanks and Delete Blank Lines

Exercise 1-18. Write a program to remove trailing blanks and tabs from each line of input, and delete entirely blank lines. The challenge here is that you cannot know whether a blank or tab is “trailing” until you have seen everything that follows it on the same line. The solution splits the work into two …

K&R C Exercise 1-17: Print Lines Longer Than 80 Characters

Exercise 1-17. Write a program to print all input lines that are longer than 80 characters. Approach This exercise is a direct application of the getline / main pattern introduced in K&R Section 1.9 — but now with a filter condition added to main. The key insight is that getline already returns the line length, …

K&R C Exercise 1-16: Print True Length of Long Input Lines

Exercise 1-16. Revise 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. The K&R longest-line program (Section 1.9) stores each input line in a fixed buffer of MAXLINE characters. That design silently truncates any line longer than …