Comparing characters

Hello,
I have a file like the following:

Where The Mind ~ Is Without Fear

I wrote the following program to compare characters and find the largest character:

function compare()
    inn = open("input.txt","r")
    ch = typemin(Int)
    while !eof(inn)
        if ch <  Int(read(inn, Char))
            ch = Int(read(inn, Char))
        end
    end
    println(Char(ch))
end

compare()

The program prints the character u instead of the character ~.

Where is the problem with my program?

Thank you.

You compare the current largest (ch) to the character you read. Then you set ch to the next character you read. So when you come to ~ you set ch to the next character , and so on. You should do

c = Int(read(inn, Char))
if ch < c
    ch = c
end

And, besides, you don’t have to convert the characters to Int, you can compare characters directly.

julia> 'u' < '~'
true
1 Like

Hi,
Thank you so much.