# Comparing characters

**URL:** <https://discourse.julialang.org/t/comparing-characters/123442>\
**Category:** New to Julia\
**Tags:** char\
**Created:** [December 4, 2024, 9:44am UTC](https://discourse.julialang.org/t/comparing-characters/123442 "2024-12-04T09:44:23Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![hack3rcon](https://avatars.discourse-cdn.com/v4/letter/h/96bed5/32.png) [@hack3rcon](https://discourse.julialang.org/u/hack3rcon)\
**Post date:** [December 4, 2024, 9:44am UTC](https://discourse.julialang.org/t/comparing-characters/123442/1 "2024-12-04T09:44:23Z")

</div>

Hello,  
I have a file like the following:

```julia
Where The Mind ~ Is Without Fear

```

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

```julia
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.

---

<div class="post-metadata">

**Author:** ![sgaure](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sgaure/32/14779_2.png) [@sgaure](https://discourse.julialang.org/u/sgaure)\
**Post date:** [December 4, 2024, 9:57am UTC](https://discourse.julialang.org/t/comparing-characters/123442/2 "2024-12-04T09:57:26Z")

</div>

> [@hack3rcon](#):
>
> ```julia
> if ch < Int(read(inn, Char))
> ch = Int(read(inn, Char))
> end
> 
> ```

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

```julia
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
julia> 'u' < '~'
true

```

---

<div class="post-metadata">

**Author:** ![hack3rcon](https://avatars.discourse-cdn.com/v4/letter/h/96bed5/32.png) [@hack3rcon](https://discourse.julialang.org/u/hack3rcon)\
**Post date:** [December 7, 2024, 6:58am UTC](https://discourse.julialang.org/t/comparing-characters/123442/3 "2024-12-07T06:58:49Z")

</div>

Hi,  
Thank you so much.
