Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 help with palindrome question
Index -> Programming, Turing -> Turing Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
JR




PostPosted: 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.
Sponsor
Sponsor
Sponsor
sponsor
AsianSensation




PostPosted: Mon Apr 19, 2004 4:18 pm   Post subject: (No 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
EDEN-E




PostPosted: Mon Apr 19, 2004 4:41 pm   Post subject: (No 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.
Tony




PostPosted: Mon Apr 19, 2004 6:05 pm   Post subject: (No 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
Latest from compsci.ca/blog: Tony's programming blog. DWITE - a programming contest.
Display posts from previous:   
   Index -> Programming, Turing -> Turing Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 4 Posts ]
Jump to:   


Style:  
Search: