Computer Science Canada

help with palindrome question

Author:  JR [ Mon Apr 19, 2004 4:12 pm ]
Post subject:  help with palindrome question

code:

function palindrome (s : string) : boolean
    if s (1) = s (*) then
        palindrome (s (2 .. * -1))
    end if
end palindrome
var s : string
put "Enter a word"
get s
if palindrome (s) = true then
    put s, "is a palindrome"
else
    put s, "is not a palindrome"
end if


this is the code, i need to make the program find the palindrome using recustion. I need help fixin the prog, some strange errors.

Author:  AsianSensation [ Mon Apr 19, 2004 4:18 pm ]
Post subject: 

well, right now you are calling a function like a procedure, you can't do that.

Another error, is that when you are passing the string as a parameter, if the string you pass is 1 character long, passing s (2 .. * - 1) will give you error.

that's about it, from what I see

Author:  EDEN-E [ Mon Apr 19, 2004 4:41 pm ]
Post subject: 

code:
function palindrome (s : string, i : int) : boolean
    if length(s) = i then result true end if

    if s(i) = s(length(s)-i+1) then
        if palindrome (s, i + 1) then
            result true
        end if
    end if
     
    result false
end palindrome

var s : string
put "Enter a word"
get s

if palindrome (s, 1) = true then
    put s, " is a palindrome"
else
    put s, " is not a palindrome"
end if


hmm... when you use function, you have to return the result.

Author:  Tony [ Mon Apr 19, 2004 6:05 pm ]
Post subject: 

if a function returns boolean, you don't need to use comparison operator in if statement Confused
code:

function example():boolean
return true
end example

if example then
put "since function returned true, the overall if statement condition is also true"
end if


: