middle letter?
Author |
Message |
pokerface
|
Posted: Tue Oct 26, 2004 4:12 pm Post subject: middle letter? |
|
|
im having problems getting any idea on how to get the middle letter of a word!
example:
MONKEY then middle letter is none cause its even
Madam then middle letter is d
i have no idea on how to get that to work! could u give me some idea on how to start it? |
|
|
|
|
|
Sponsor Sponsor
|
|
|
Tony
|
Posted: Tue Oct 26, 2004 5:05 pm Post subject: (No subject) |
|
|
code: |
var text : string := "Madam"
put text (round (length (text) / 2))
|
|
Tony's programming blog. DWITE - a programming contest. |
|
|
|
|
pokerface
|
Posted: Tue Oct 26, 2004 5:13 pm Post subject: (No subject) |
|
|
code: | var word, midletter : string
put "word"
get word
for i : 1 .. length (word)
midletter := word
put midletter (length (word) / 2)
end for
|
it dont work cause i took out round and now its not working? what may be the problem? |
|
|
|
|
|
templest
|
Posted: Tue Oct 26, 2004 5:35 pm Post subject: (No subject) |
|
|
code: | var word : string := "antidisestablishmentarianism"
var buffer : array 1 .. length (word) of string
var donkey : string := ""
for i : 1 .. length (word)
buffer (i) := word (i)
end for
for r : 1 .. length (word)
color (r * 4)
put chr (ord (buffer (r)))..
end for
locate (3, 1)
for z : 1 .. length (word)
donkey := donkey + buffer (z)
end for
donkey := donkey (round (length (donkey) / 2))
color (green)
put donkey |
|
|
|
|
|
|
pokerface
|
Posted: Tue Oct 26, 2004 7:15 pm Post subject: (No subject) |
|
|
that doesnt work thou? what was that any ways? |
|
|
|
|
|
AsianSensation
|
Posted: Tue Oct 26, 2004 7:48 pm Post subject: (No subject) |
|
|
yeah, it was templest's attempt at helping.
to get the middle letter, do a simple if statement, if length (word) mod 2 = 0 then there is no middle letter, because it's even.
else, the middle letter is word (length (word) div 2 + 1), because length div 2 rounds down, add 1 to it because the substring starts with 1 as the index (I think that made sense....) |
|
|
|
|
|
pokerface
|
Posted: Tue Oct 26, 2004 8:23 pm Post subject: (No subject) |
|
|
thanks but i did what u said! im getting better at this! hehe! but i am getting an error with this program
code: |
var word : string
var pattern1, pattern2 : string := ""
put "Enter words, patterns or end with 'exit' to end program."
put ""
loop
put "Enter a word."
get word
put "Enter the pattern you want to find."
get pattern1
put "Enter the pattern you want to replace it with."
get pattern2
exit when word = "*"
for i : 1 .. length (word)
if index (word, pattern1) not= 0 then
put word, "contains ", pattern1, "."
put pattern2, " replaces", pattern1, "and the new word is ... "
word := word (1 .. index (word, pattern1) - 1 + word (index (word, pattern1) + 1 .. *)
put word
else
put word, " does not contain ", pattern1, "."
end if
end for
end loop
|
could u see what may be changed to make it work this line is the one giving me the most problems code: | word := word (1 .. index (word, pattern1) - 1 + word (index (word, pattern1) + 1 .. *)
put word | ty |
|
|
|
|
|
AsianSensation
|
Posted: Tue Oct 26, 2004 8:37 pm Post subject: (No subject) |
|
|
well, I see you are trying to replace a substring with another substring, well, do something like this:
find the location of the substring in the original string using index, then sandwich the new substring with all the stuff before and after the original substring. Confused? Good, here is an example:
code: | var word := "AsianSensation"
var oldSubstr := "Sen"
var newSubstr := "Hello"
var newword : string
var pos := index (word, oldSubstr)
newword := word (1 .. pos - 1) + newSubstr + word (pos + length (oldSubstr) .. *)
put newword |
This is one of the easier approach, because it doesn't solve multiple occurences. Think on it first, then make your own. |
|
|
|
|
|
Sponsor Sponsor
|
|
|
templest
|
Posted: Wed Oct 27, 2004 6:52 am Post subject: (No subject) |
|
|
wtf? How the hell does that no work? I just ran it on my box right now without any problems. Do you have turing 4.0.5? My code works, you pinko commie.
It was a joke too. Mine does the exact same thing that tony's does, only that with 15 lines of useless code. (If you count the pretty colours as useless code, which I certainly do not). |
|
|
|
|
|
wtd
|
Posted: Wed Oct 27, 2004 10:08 pm Post subject: (No subject) |
|
|
I have no idea why this is such a controversial topic.
code: | var word := "hello"
if length(word) mod 2 = 0 then
put "Can't be done with a word with an even number of letters!"
else
put "The character in the middle is: " + word(length(word) div 2 + 1)
end if |
Oh, and Templest... throwing around unnecessary insults doesn't help anything. |
|
|
|
|
|
|
|