Understanding Tab Stops in C (K&R Exercise 1-20)
My favourite programming language is C. I really like C because it helps me understand what actually happens under the hood. Unlike other high-level languages, C gives a more direct connection to how the system works, and that’s something I find very interesting.
Recently, I started reading the classic K&R book (The C Programming Language), which was recommended by my Guru. While going through it, I came across a problem in Chapter 1, Exercise 1-20, and decided to work on it.
In this article, I’ll walk through my thought process and how I approached solving this problem.
Understanding the Problem
Let’s consider the tab width as 8 spaces.
You can actually have any number of spaces for a tab width. According to the question, we need to assume a fixed number of tab stops every n columns. For example, if n = 8, then the tab stops are multiples of 8, i.e., (8, 16, 24, 32, …), and so on.You can imagine a line after every 8 columns.
The input for the program would be a string, and the goal is to replace every \t character in the input string with the correct number of spaces so that the next character starts at the next tab stop.And if you come across a \n character, the column must be reset to 0 since it begins with a new line.
To understand this better, consider the string:
s = “Hi\tSneha”
After printing "Hi", we are at the 3rd column. The next tab stop is at the 8th column.So, we need to fill spaces until we reach the 8th column.Therefore, we would be filling 6 spaces to reach the next tab stop.
I hope you now have a clear understanding of what the input and output should be.
Now, let’s move step by step towards the solution.
Algorithm (Pseudocode)
Loop through every character of the string.
If the current character is '\t', then:
total_spaces = 8 - (column % 8)
while space > 0:
print space
space--
column++
Else:
print current character
column++
If the current character is '\n', then reset:
column = 0
In the above pseudocode, the column variable is initially set to zero, and we loop through every character in the input string s. If the current character is ‘\t’, then we need to calculate the total number of spaces to print, which is given by 8 - (column % 8).
If we encounter a character other than ‘\t’, we simply print the current character and increment the column variable. In fact, every time a character is printed onto the screen, we must increment the column variable, since it keeps track of the current column position.
The implementation of the above algorithm is given below. Before you jump into the solution, I highly recommend trying it out yourself—it really helps in understanding the logic better.
void detab(char *input) {
int total_spaces, column = 0;
while (*input) {
if (*input == '\t') {
total_spaces = 8 - (column % 8);
while (total_spaces-- > 0) {
putchar(' ');
column++;
}
} else {
printf("%c", *input);
column++;
if (*input == '\n') {
column = 0;
}
}
input++;
}
}
Hope this helps, and happy coding! 🚀