Computer Science Canada String Manipulation |
Author: | maincharName [ Sat Mar 18, 2006 9:34 pm ] |
Post subject: | String Manipulation |
Is there an easy way to identify a specific word within a string? You see, I want the program to identify words such as "take" or “key” within a sentence no matter its placement. If this is possible, then a person could type variations of the same sentence and get the same result. For example, whether you type “take key”, “take the key”, or “Well, I see that key sitting on the ground. Perhaps I will take it.” you will get the same message saying, “you have taken the key.” PS: Is it possible to have a string variable represent more than one value? Could it represent “take”, “get” and “pick up” all at once? Thank you for your time. |
Author: | person [ Sat Mar 18, 2006 9:45 pm ] |
Post subject: | |
maincharname wrote: Is there an easy way to identify a specific word within a string? You see, I want the program to identify words such as "take" or "key" within a sentence no matter its placement.
If this is possible, then a person could type variations of the same sentence and get the same result. For example, whether you type "take key", "take the key", or "Well, I see that key sitting on the ground. Perhaps I will take it." you will get the same message saying, "you have taken the key." I don't know if this would be the best solution, but you could sort all of the words in a sentence in an alphabetic order. For example. "It is a nice day today." would be sorted to: "a day is it nice today." You could then perform a search to see if the strings that you are looking for are present in the sentence. maincharname wrote: PS: Is it possible to have a string variable represent more than one value? Could it represent "take", "get" and "pick up" all at once?
Yes. Look up "Records". |
Author: | maincharName [ Sat Mar 18, 2006 10:04 pm ] |
Post subject: | |
Thank you, that helps a little. But how do I create a search? |
Author: | Tony [ Sat Mar 18, 2006 10:05 pm ] | ||
Post subject: | |||
index() is your friend - the function finds a substring in a larger string and returns its location. so
it is important to have spaces surrounding your word, otherwise other words such as 123key456 will produce an unwanted result. As such, you also need to add spaces to beginning/end of your sentance (to check for first/last word) and lower/up-case everything, as "Key" is not the same as "kEy" |
Author: | maincharName [ Sat Mar 18, 2006 10:27 pm ] | ||||
Post subject: | |||||
It works perfectly when the text is predefined:
But what did I do wrong here?
|
Author: | Tony [ Sat Mar 18, 2006 10:29 pm ] | ||
Post subject: | |||
what's the value of answ? |
Author: | maincharName [ Sat Mar 18, 2006 10:32 pm ] |
Post subject: | |
Same as in the one that worked: take the key |
Author: | [Gandalf] [ Sat Mar 18, 2006 10:37 pm ] | ||||
Post subject: | |||||
This allows you to have spaces in your input. It will allow anything up to 255 characters in length.
This will not work because it is searching for a space after "key", which it will not find because your string ends at 'y'. Remove this space and it will work correctly. |
Author: | Tony [ Sat Mar 18, 2006 10:39 pm ] | ||||||
Post subject: | |||||||
maincharName wrote: Same as in the one that worked: take the key
no it's not
hmm... perhaps you need to read the whole sentance, and not just the first word ![]()
![]() edit: in a sentance such as "I am keyless" the program will think that you have a key. So just
to add space buffers to beginning and end |
Author: | [Gandalf] [ Sat Mar 18, 2006 10:41 pm ] |
Post subject: | |
Tut, tut. What I find most interesting is that he had a post after: Quote: Same as in the one that worked: take the key
Which explained that he removed the spaces from the string, or something along those lines. |
Author: | maincharName [ Sat Mar 18, 2006 10:43 pm ] | ||||||
Post subject: | |||||||
maincharName wrote: It works perfectly when the text is predefined:
But what did I do wrong here?
Sorry about that, I'm a bit tired. To clerify, the first code didn't work when there was a space before and after the word key in quotations: " key " so I changed it to: "key" Unfortunately, the...
...still doesn't work |
Author: | Tony [ Sat Mar 18, 2006 10:43 pm ] |
Post subject: | |
[Gandalf] wrote: Quote: Same as in the one that worked: take the key
Which explained that he removed the spaces from the string, or something along those lines. It also tells me that he hasn't bothered to check what the actual value was, because clearly it is not the same ![]() |
Author: | maincharName [ Sat Mar 18, 2006 10:45 pm ] |
Post subject: | |
Yes, it was the same! I just neglected to completely paste the code. That problem was in the forum, not the code. |
Author: | Tony [ Sat Mar 18, 2006 10:49 pm ] | ||
Post subject: | |||
maincharName wrote: That problem was in the forum, not the code.
Unbelivable..
If you're going to tell me that the value of answ is "take the key", you'd have to upload a screenshot. |
Author: | maincharName [ Sat Mar 18, 2006 10:55 pm ] | ||
Post subject: | |||
What? Ok, now I'm getting frustrated. Please just try it yourself if you don't believe me. Please copy the following code into turing, run it and type, "take the key" (without the quotations of course) and see whether it gives you the same results as the predefined version.
I actually do care and have had turing open the whole time. |
Author: | Tony [ Sat Mar 18, 2006 10:56 pm ] |
Post subject: | |
show me a screenshot with answ's value. |
Author: | maincharName [ Sat Mar 18, 2006 11:01 pm ] |
Post subject: | |
and here is your screen shot, I know it's small but you can clearly see that it says "sorry, what?" and not "you have taken the key" |
Author: | Cervantes [ Sat Mar 18, 2006 11:02 pm ] | ||||
Post subject: | |||||
[Gandalf] wrote:
This allows you to have spaces in your input. It will allow anything up to 255 characters in length. Actually, it allows more than 255 characters, doesn't it? Which, of course, causes an error. But the point is, it won't stop you at 255 characters, as
will. IIRC. |
Author: | Tony [ Sat Mar 18, 2006 11:06 pm ] |
Post subject: | |
maincharName wrote: you can clearly see that it says "sorry, what?" and not "you have taken the key"
you're talking about the outcome of your if statement. I was asking you for Tony wrote: show me a screenshot with answ's value.
value of answ variable, so that you can see why the if statement makes the logical decision as it does |
Author: | [Gandalf] [ Sat Mar 18, 2006 11:06 pm ] |
Post subject: | |
Cervantes, Cervantes... Of course, you are right... I meant that it will not allow you to continue with the program, causing a run time error. maincharName, why are you not using get :*? The code you most recently posted will say "Sorry, what?" because the only word which is put into answ variable is "take". Use get :* to solve this. |
Author: | maincharName [ Sat Mar 18, 2006 11:17 pm ] |
Post subject: | |
thank you Gandalf! <tears stream down face> I'm sorry I missed that post. It's getting late and it was just above my half closed eye lids. Problem solved! Thank you everyone! |