
-----------------------------------
blankout
Tue Apr 14, 2009 3:36 pm

inclusive if statements
-----------------------------------
I was wondering is it's possible to have an if statement that is inclusive, such as


if answer="y"
then
%...


is it possible to have the user input a word and as long as the word has the letter y in it, the program will run a specific output?

-----------------------------------
Tony
Tue Apr 14, 2009 3:46 pm

RE:inclusive if statements
-----------------------------------
you might be interested in [tdoc]index[/tdoc]

-----------------------------------
blankout
Tue Apr 14, 2009 3:54 pm

Re: inclusive if statements
-----------------------------------
so i tried using index, but i don't quite get it, if you can understand what i'm trying to do from this code, can i get any help?


var word : string
get word : *
cls
put index (word, "a")
cls
if index (word, "a") then
put word(1..index(word, "a")-1)..
put "i"(index(word, "a")..length (word))
end if


-----------------------------------
Tony
Tue Apr 14, 2009 4:07 pm

RE:inclusive if statements
-----------------------------------
index returns an integer value, not boolean (true/false).

-----------------------------------
TheGuardian001
Tue Apr 14, 2009 4:07 pm

Re: inclusive if statements
-----------------------------------
the index command returns an integer value based on where the letter was found in the string, or 0 if the letter was not found. so if we only need to know if the letter was found, what result would we check for?

Edit: Ninja'd by Tony.

-----------------------------------
blankout
Tue Apr 14, 2009 4:09 pm

Re: inclusive if statements
-----------------------------------
well i'm trying to replace all 'a' s in a sentence with 'i's, so what would i need to do in order to produce that result

-----------------------------------
Tony
Tue Apr 14, 2009 4:37 pm

RE:inclusive if statements
-----------------------------------
Start with simplifying the problem. Your teacher might have been saying something like that -- breaking problems down into smaller problems. "Command & Conquer", or something lame like that.

My favourite approach to such problems is recursion / induction.

All you have to do is figure out two things:
1. What is the most basic case for this problem.
2. How can you get one step closer to such a base case.

And those two steps are enough for any size of a problem, 1 to infinity (limited by hardware and time ;) )


1. What is the shortest sentence you can think of, where you would need to replace 'a'?
2. Given a sentence of any length (longer than one in #1), how can you get it to be one letter shorter?
