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

Username:   Password: 
 RegisterRegister   
 Handling Strings - Help
Index -> Programming, Turing -> Turing Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
globSter




PostPosted: Wed Dec 07, 2005 9:09 pm   Post subject: Handling Strings - Help

I'm just taking Turing for the first time in grade 10, and we've been given an assignment to create a program that is reponsible for returning a word entered backwards.

As in, if the user was to enter:

MAN

the result would be:

NAM

however, if the user enters a palindrome such as WOW, then the output displays "This is a palindrome" on the screen.

Now, I've worked with similar programming codes before, but I'm not quite sure if I'm on the right track. I pretty new to programming so this is probably a really basic question, but any help would be greatly appreciated.

The Glob.
Sponsor
Sponsor
Sponsor
sponsor
Cervantes




PostPosted: Wed Dec 07, 2005 9:30 pm   Post subject: Re: Handling Strings - Help

globSter wrote:

Now, I've worked with similar programming codes before, but I'm not quite sure if I'm on the right track.


Post what you've got and/or post your algorithm.

decreasing for loops might help you out here.

code:

for decreasing i : 10 .. 1
     put i
end for
DIIST




PostPosted: Wed Dec 07, 2005 9:30 pm   Post subject: (No subject)

You need to know how to do basic string manipulation to find the word entered backwords. Do do this, this tutorial might be of some help.
http://www.compsci.ca/v2/viewtopic.php?t=8229 Once you know how to manipulate strings, the problem should seem a lot easier.
globSter




PostPosted: Wed Dec 07, 2005 10:06 pm   Post subject: Similar Codes

These are some of the codes that I've worked with ... and will post another tomorrow.

code:
put "Please enter 10 words"         

for k: 1 .. 10                     
    get word                       
    line := line + word + " "       
end for                             

put "\nThe total number of characters in the given words are ", length(line), "."
                                   

avglength := length(line)/10       
put "\nThese 10 words have an average word length of ",avglength, " letters."


Another program:

code:
%To display the first and last letter of a given word.

put "Please enter words one to a line. End with 'quit'."
                                   
delay (1000)                       
   
loop                               
    put "\nPlease enter a word."   
    get word                       
    exit when word = "quit"         
    if length(word) >= 2 then       
        put word (1) ..             
        put word (*)               
    else                           
        put "This word only has one character."
    end if                         
end loop


Thanks for the for decreasing loop suggestion, and I will try it. I have another program that I created using ord and chr codes. Will post tomorrow.
Cervantes




PostPosted: Thu Dec 08, 2005 2:03 pm   Post subject: Re: Similar Codes

globSter wrote:

code:
put "Please enter 10 words"         

for k: 1 .. 10                     
    get word                       
    line := line + word + " "       
end for                             

put "\nThe total number of characters in the given words are ", length(line), "."

This is not quite correct. The total number of characters in the given words is length (line) - 10, since you added a character (space) for each word.
globSter




PostPosted: Thu Dec 08, 2005 7:31 pm   Post subject: Program

Thankyou for the correction. I was able to solve my program problem. Thanks all for the help.
Thuged_Out_G




PostPosted: Fri Dec 09, 2005 11:44 pm   Post subject: (No subject)

i really suck at string manipulation, and i was reading this topic ... figured id give it a try

im sure there is a much better way to do this, but heres what i came up with

code:

loop

    var word : string
    put "\nEnter a word to flip it(type exit to quit): "..
    get word
    if word = "exit" then
        exit
    end if

    var reverse : array 1 .. length (word) of string
    var right : array 1 .. length (word) of string
    var count : int := 0
   
    for i : 1 .. length (word)
        reverse (i) := word (1 + length (word) - i)
        right (i) := word (i)
    end for

    for i : 1 .. length (word)
        if right (i) = reverse (i) then
            count += 1
            if count = length (word) then
                put "you entered a pallindrom"
            end if
        end if
    end for

    for i : 1 .. length (word)
        if count not= length (word) then
            put reverse (i) ..
        end if
    end for
end loop

put "Thanks for trying!"

Cervantes




PostPosted: Sat Dec 10, 2005 8:41 am   Post subject: (No subject)

No need to store things in an array.

code:

    for i : 1 .. length (word)
        if right (i) = reverse (i) then
            count += 1
            if count = length (word) then
                put "you entered a pallindrom"
            end if
        end if
    end for

Two things, here. First, you only need to check up to floor(length (word) / 2). Second, the only way count could equal length (word) is if count has been incrimented each and every time. There's no point checking if count equals length (word) each time through your for loop because there is only one time in that for loop when they could possibly be equal. The value of count is not going to change, so you can easily move that if statement outside the for loop.

code:

    for i : 1 .. length (word)
        if count not= length (word) then
            put reverse (i) ..
        end if
    end for

count and length (word) are fixed. There is no point comparing them each time through the loop. Move that if statement outside the loop to improve performance, and for better reasoned coding. Furthermore, if that condition was false at some point, you would miss letters from reverse. You wouldn't want that!

Here's mine:
Turing:

fcn is_palindrome (phrase : string) : boolean
        var len := length (phrase)
        for i : 1 .. len div 2
                if phrase (i) not= phrase (len - i + 1) then
                        result false
                end if
        end for
        result true
end is_palindrome

var input_phrase : string
get input_phrase
put "T/F: ",input_phrase, " is a palindrome: ", is_palindrome (input_phrase)
Sponsor
Sponsor
Sponsor
sponsor
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  [ 8 Posts ]
Jump to:   


Style:  
Search: