Your code is not the same as the example you reference.
The example you reference only replaces four spaces with a tab when four consecutive spaces occur.
You appear to want a program to replace the spaces between 'words' with a tab if the 'word' starts on or after a multiple of a tab stop.
Two different requirements.
You say that 5 characters, plus two spaces, plus one character should result in replacing the two spaces with a tab as the one character, the second 'word', starts on a tab stop.
At
while (x / tabstop != (x + spaces) / tabstop)
the value of x
is 5, 5/4=1. The value of spaces is 2, (5+2)/4 = 1. Equal. No tab. Program is doing what it is programmed to do.
You want to replace the two spaces with a tab because the 8th character starts a word.
Your version of the example does not do that. x
has the value 7.
You appear to want x
to have the value 8. 5/4 = 1, 8/4 = 2, substitute a tab.
Change the program to include the current character in the count when you have two or more spaces and see what happens.