-2

Exercise 1-21. Write a program 'entab' that replaces strings of blanks by the minimum number of tabs and blanks to achieve the same spacing. When either a tab or a single blank would suffice to reach a tab stop, which should be given preference?

What does the bolded part mean, please explain with an example.

  • similar to [K&R Exercise 1-20 - Need some clarification](https://stackoverflow.com/questions/7178201/kr-exercise-1-20-need-some-clarification/), where tab stops are explained – Sander De Dycker Sep 06 '18 at 07:55
  • OK, you did not accept that your prior question on this problem was a duplicate of another question, but this new post _really is_ a duplicate of your prior question. – halfer Sep 08 '18 at 20:11

1 Answers1

1

The bolded question is basically asking you to think about the special case when a tab would only replace a single character. Is it then worth it / necessary to replace that space with a tab ?

Example (with tab stops - indicated by ^ - every 3 characters) :

hello world
^  ^  ^  ^

There's a single space between the two words, that can be replaced with a single tab ("hello\tworld") to achieve the same spacing. Do you replace it ?

Similarly :

hello    world
^  ^  ^  ^  ^

There are 4 spaces between the two words, that can be replaced with two tabs ("hello\t\tworld"), or with a space and a tab ("hello \tworld"), to achieve the same spacing. Which do you choose ?

Sander De Dycker
  • 16,053
  • 1
  • 35
  • 40
  • I'm sorry, but i didn't understand your explanation. – shaik nisar ahmed Sep 06 '18 at 08:37
  • @shaiknisarahmed : which part did you not understand ? I assumed you understood the first sentence in the exercise, which means you understand what spaces and tabs are, and hopefully also what tab stops are (if not, please have a read through my answer to the question I [linked in comment](https://stackoverflow.com/questions/7178201/kr-exercise-1-20-need-some-clarification/)). – Sander De Dycker Sep 06 '18 at 08:41
  • I know what a tabstop is, i know what a space is and a tab is. What i don't understand is the special case in the question. Can you please elaborate your explanation and simplify in laymen terms! – shaik nisar ahmed Sep 06 '18 at 08:49
  • @shaiknisarahmed : I included the two examples for that purpose. I don't think I can come up with simpler examples than that. I suggest you just think about the two cases I presented, and which of the spaces you would replace with tabs. First for the string `"hello world"` when tab stops are every 3 characters, then for the string `"hello world"` (imagine 4 spaces between the two words - for some reason they got removed), also when tab stops are every 3 characters. – Sander De Dycker Sep 06 '18 at 08:53
  • tabstop -> '#' , tab -> '$' , space -> '*' and tabstop = 8. h e y l l o o * c a l l e r $ * There's a space at the first tabstop, and a combination of a tab and a space. Can the example be considered as a special case? – shaik nisar ahmed Sep 06 '18 at 08:56
  • In the first example, a space occurs at the 6th character, which is a tabstop, so you replace it with a tab, and in the second example, a space occurs at 6th and 9th character and you replace it with a tab. Note: 7th and 8th character is also a space, but i have ignored it. And just because a space occurs at tabstop and if replaced by a tab, how will that end up with same spacing? and in the 2nd example, 6th, 7th,8th,9th positions are characters and the 4 consecutive spaces can be replaced by a tab, but 6th & 9th pos is space, & if replaced with tabs, that doesn't end up in same spacing. – shaik nisar ahmed Sep 06 '18 at 09:02
  • @shaiknisarahmed : you actually replace the 7th, 8th and 9th character with a tab (so, you replace 3 spaces with a single tab - you don't leave the 7th and 8th spaces in there). So, in the first case, you replace a single space with a tab, and in the second case you replace a single space with a tab, and then 3 spaces with a tab. The bolded question is asking you specifically if replacing a single space with a tab is needed, or whether you can just leave it as a space. – Sander De Dycker Sep 06 '18 at 09:08
  • @shaiknisarahmed : no - your explanation indicates a basic misunderstanding of what tabs and tab stops are, which I attempted to rectify in my response. Clearly without success. A tab is horizontal spacing that jumps to the next tab stop. Tab stops typically occur at fixed intervals (eg. every 3 characters in my examples). If the next tab stop is 3 characters away, then a tab jumps 3 characters forward. If the next tab stop is 2 characters away, then a tab jumps 2 characters forward. If the next tab stop is 1 character away, then a tab jumps 1 character forward (just like a space). – Sander De Dycker Sep 06 '18 at 09:16