The function is supposed to return true only if all letters have "+" on both sides, but I always get true as my value... What's the flaw in my code?
def SimpleSymbol?(str)
str_array = str.split("")
i = 0
while i < str_array.length-1
if ((str_array[i] >= "a") && (str_array[i] <= "Z"))
if ((i == 0) || (i == str_array.length-1))
return false
end
if ((str_array[i-1] != "+") && (str_array[i+1] != "+"))
return false
end
end
i += 1
end
return true
end
puts SimpleSymbol?("ads++d+")
"Z" is smaller than "a".
If both sides must to be
+
, and you check for mismatch, the logical connection is OR (= if either of those things are true).note: you don't need to
split
, you can index into strings.