0

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?

Rob Ye
  • 63
  • 1
  • 7

1 Answers1

2

When you read the first space character, don't print it right away. Wait for the next non-space character to print it and if you get the EOL character, you don't print it. Here is your algorithm modified:

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
    has_read_space := False
    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
            if has_read_space then
                output.putchar (Space_char) -- Print the space when reading a character
                has_read_space := False
            end
            output.putchar (ch)
            flag := 1
            read_char
        elseif ch = Space_char and flag = 1 then    -- see space after characters
            has_read_space := True  -- Don't print it right now
            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
Louis M
  • 576
  • 2
  • 4