Here is the my Eiffel Code for my space removal assignment:
feature {NONE} -- Main routine
copy_file
-- Copy a file character by character from input to output
require
input_open: input.is_readable
output_open: output.is_writable
local flag: INTEGER
do
flag := 0 -- 0 for previous space, 1 for previous char
from read_char -- Must prime the pump by reading the first character
until ch = EOF
loop
from
ch := input.last_character
until
ch = EOL
loop
if ch = Space_char and flag = 0 then -- leading spaces
read_char
elseif ch /= Space_char and flag = 0 then -- see first charater after space
output.putchar (ch)
flag := 1
read_char
elseif ch = Space_char and flag = 1 then -- see space after characters
output.putchar (Space_char)
flag := 0
read_char
elseif ch /= Space_char and flag = 1 then -- see character after character
output.putchar (ch)
read_char
end
end
flag := 0
read_char
end
-- At end of file, nothing to do in Eiffel except close the files
input.close
output.close
end
Here is the sample input:
Leading spaces
Training spaces
Leading and trailing spaces
Only interword spaces
Leading, trailing and interword spaces
This line has correct spaces
Previous line was empty
Previous line has only leader spaces
OneWordLine
Three word line
I ran the code and I get the output which is slightly different from the requirement, say, I always get a extra space when there is a tailing spaces in the line.
Here is my output:
Leading spaces
Training spaces
Leading and trailing spaces
Only interword spaces
Leading, trailing and interword spaces
This line has correct spaces
Previous line was empty
Previous line has only leader spaces
OneWordLine
Three word line
Can someone help me with that?