0

Here is my eiffel program, which is basically doing space removal (remove the redundant spaces in a given text file) to follow the regular expresson: A+(SA+)*EOL, where for each line in the file, it must start with alphabets and only one spaces between alphabets.

My question is, base on this program, how can I extend it to make every other word to be uppercase? i.e. 2th, 4th, 6th, etc.

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
has_read_space: BOOLEAN
empty_line : BOOLEAN
do
empty_line: True
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
            if has_read_space then          -- this clause make sure the space will only place in between two words instead of end of lin
               output.putchar (Space_char)
            end
            output.putchar (ch)
            empty_line := False
            flag := 1
            read_char
        elseif ch = Space_char and flag = 1 then    -- see space after characters
            has_read_space := True          -- Don't output it right away
            flag := 0
            read_char
        elseif ch /= Space_char and flag = 1  then  -- see character after character
            output.putchar (ch)
            read_char
        end
    end
    if empty_line = False then
        output.putchar (EOL)        -- if line is not empty, then we place EOL at the end of line
    end
    flag := 0       -- reset it to 0 to make sure the next follow the same routine
    has_read_space := False -- reset it to avoid placing space in the beginning of line
    empty_line := True  -- reset to proceed to new line
    read_char
end

  -- At end of file, nothing to do in Eiffel except close the files
input.close
output.close
end
ctrl-alt-delor
  • 7,506
  • 5
  • 40
  • 52
Rob Ye
  • 63
  • 1
  • 7

1 Answers1

0

Just figured it out. Simply set another flag, say evenWord, initialize it to be false. and in the elseif ch = Space_char and flag = 1 clause flip the flag evenWord := not evenWord It works

Rob Ye
  • 63
  • 1
  • 7