Reading and printing characters from a file

Hello,
I have a file like below:

A B c

I want to write a program that produces output like the following:

A is Uppercase
B is Uppercase
c is Lowercase

I wrote the following program:

function UL()
    inn = open("input.txt","r")
    while !eof(inn)
        if isuppercase(read(inn, Char))
            println("$(read(inn, Char)) is Uppercase")
        else
            println("$(read(inn, Char)) is Lowercase")
        end
    end
end

UL()

But output is:

is Uppercase
is Uppercase
is Lowercase

Why?

Thank you.

read(inn, Char) reads a single Char from the input stream. You call read once in the if condition, which reads the letters from your file, and then you call read the second time when printing, which reads the spaces from your file.

3 Likes

Hi,
Thank you.
The corrected version is:

function UL()
    inn = open("input.txt","r")
    while !eof(inn)
        c = read(inn, Char)
        if isuppercase(c) && c!=' '
            println("$c is Uppercase")
        elseif islowercase(c) && c!=' '
            println("$c is Lowercase")
        end
    end
end
UL()