Author |
Message |
baby_bubblz
|
Posted: Tue Nov 23, 2004 8:43 pm Post subject: reversing words |
|
|
hey can someone help me fill in the ??????? with reversing a word the user inputs? it'd be really helpful. thanks!
i think part of it goes something like this:
var word: string
var reverse: string
put "enter a word"
get word
for decreasing i: length(word) .. 1
????????????????
and i think there's something like word(length(word))?
end for
"the reversed word is " , put reverse |
|
|
|
|
|
Sponsor Sponsor
|
|
|
Cervantes
|
Posted: Tue Nov 23, 2004 9:00 pm Post subject: (No subject) |
|
|
Right-o! Determining that you need to use a for loop and the length() function was the hard part!
So, if you want to simply output the reversed word on the screen, try this:
code: |
var word : string
get word
put "the word reversed is " ..
for decreasing i : length (word) .. 1
put word (i) .. %word (i) refers to the character in the string varaible, "word", at position i.
end for
|
If you actually want to save it to a variable, try this:
code: |
var word : string
get word
var reversed : string := ""
for decreasing i : length (word) .. 1
reversed += word (i) %note: this is the same as reversed := reversed + word (i)
end for
put "the word reversed is ", reversed
|
I hope that helped. |
|
|
|
|
|
wtd
|
Posted: Tue Nov 23, 2004 9:18 pm Post subject: (No subject) |
|
|
Also, consider the recursive solution:
code: | function reverseString (s : string) : string
if s = "" then
result s
else
result s (length (s)) + reverseString (s (1 .. length (s) - 1))
end if
end reverseString |
|
|
|
|
|
|
zylum
|
Posted: Tue Nov 23, 2004 11:29 pm Post subject: (No subject) |
|
|
or simply :
code: | for decreasing i : length (word) - 1 .. 1
word := word (1 .. i - 1) + word (i + 1 .. *) + word (i)
end for |
|
|
|
|
|
|
con.focus
|
Posted: Sun Nov 28, 2004 7:52 pm Post subject: (No subject) |
|
|
code: | var word : string
var revesre : string := ""
put "type in a word: "..
get word
for decreasing i : length (word) .. 1
revesre += word (i)
end for
delay (500)
put " this is the reverse of your word"
put revesre
|
i think this is wut u were originally going for |
|
|
|
|
|
|