Taghreed Mohamed
Taghreed Mohamed

Reputation: 61

Getting the largest and smallest word at a string

when I run this codes the output is (" "," "),however it should be ("I","love")!!!, and there is no errors . what should I do to fix it ??

sen="I love dogs"

function Longest_word(sen)
  x=" "

  maxw=" "

  minw=" "

  minl=1

  maxl=length(sen)

  p=0

  for i=1:length(sen)

     if(sen[i]!=" ")
         x=[x[1]...,sen[i]...]
    else
      p=length(x)

      if p<min1

        minl=p
        minw=x
      end

      if p>maxl
        maxl=p
        maxw=x
      end
      x=" "
    end
  end


  return minw,maxw

end

Upvotes: 0

Views: 121

Answers (2)

Reza Afzalan
Reza Afzalan

Reputation: 5756

As @David mentioned, another and may be better solution can be achieved by using split function:

function longest_word(sentence)
  sp=split(sentence)
  len=map(length,sp)
  return (sp[indmin(len)],sp[indmax(len)])
end

Upvotes: 4

David P. Sanders
David P. Sanders

Reputation: 5325

The idea of your code is good, but there are a few mistakes. You can see what's going wrong by debugging a bit. The easiest way to do this is with @show, which prints out the value of variables. When code doesn't work like you expect, this is the first thing to do -- just ask it what it's doing by printing everything out!

E.g. if you put

if(sen[i]!=" ")
    x=[x[1]...,sen[i]...]
    @show x

and run the function with

Longest_word("I love dogs")

you will see that it is not doing what you want it to do, which (I believe) is add the ith letter to the string x.

Note that the ith letter accessed like sen[i] is a character not a string. You can try converting it to a string with

string(sen[i])

but this gives a Unicode string, not an ASCII string, in recent versions of Julia.

In fact, it would be better not to iterate over the string using

for i in 1:length(sen) 

but iterate over the characters in the string (which will also work if the string is Unicode):

for c in sen

Then you can initialise the string x as

x = UTF8String("")

and update it with

x = string(x, c)

Try out some of these possibilities and see if they help.

Also, you have maxl and minl defined wrong initially -- they should be the other way round. Also, the names of the variables are not very helpful for understanding what should happen. And the strings should be initialised to empty strings, "", not a string with a space, " ".

@daycaster is correct that there seems to be a min1 that should be minl.

However, in fact there is an easier way to solve the problem, using the split function, which divides a string into words.

Let us know if you still have a problem.

Here is a working version following your idea:

function longest_word(sentence)
    x = UTF8String("")

    maxw = ""
    minw = ""

    maxl = 0      # counterintuitive! start the "wrong" way round
    minl = length(sentence)

    for i in 1:length(sentence)          # or:  for c in sentence

        if sentence[i] != ' '            # or:  if c != ' '
            x = string(x, sentence[i])   #  or:  x = string(x, c)

        else

            p = length(x)

            if p < minl
                minl = p
                minw = x
            end

            if p > maxl
                maxl = p
                maxw = x
            end

            x = ""
        end
    end

    return minw, maxw

end

Note that this function does not work if the longest word is at the end of the string. How could you modify it for this case?

Upvotes: 3

Related Questions