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

Username:   Password: 
 RegisterRegister   
 String Manipulation
Index -> Programming, Turing -> Turing Help
Goto page 1, 2  Next
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
maincharName




PostPosted: 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.
Sponsor
Sponsor
Sponsor
sponsor
person




PostPosted: Sat Mar 18, 2006 9:45 pm   Post subject: (No 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".
maincharName




PostPosted: Sat Mar 18, 2006 10:04 pm   Post subject: (No subject)

Thank you, that helps a little. But how do I create a search?
Tony




PostPosted: Sat Mar 18, 2006 10:05 pm   Post subject: (No subject)

index() is your friend - the function finds a substring in a larger string and returns its location.

so
code:

if index(" your sentance here ", " key ") > 0 then
   % something about a key


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"
Latest from compsci.ca/blog: Tony's programming blog. DWITE - a programming contest.
maincharName




PostPosted: Sat Mar 18, 2006 10:27 pm   Post subject: (No subject)

It works perfectly when the text is predefined:

code:
var answ := "take the key"
if index(answ, " key ") > 0 then
  put "you have taken the key"
else
  put "sorry, what?"
end if


But what did I do wrong here?

code:
var answ : string
get answ
if index(answ, " key ") > 0 then
  put "you have taken the key"
else
  put "sorry, what?"
end if
Tony




PostPosted: Sat Mar 18, 2006 10:29 pm   Post subject: (No subject)

code:

var answ : string
get answ

what's the value of answ?
Latest from compsci.ca/blog: Tony's programming blog. DWITE - a programming contest.
maincharName




PostPosted: Sat Mar 18, 2006 10:32 pm   Post subject: (No subject)

Same as in the one that worked: take the key
[Gandalf]




PostPosted: Sat Mar 18, 2006 10:37 pm   Post subject: (No subject)

code:
get :*

This allows you to have spaces in your input. It will allow anything up to 255 characters in length.

code:
var answ : string
get answ :*
if index(answ, " key ") > 0 then
  put "you have taken the key"
else
  put "sorry, what?"
end if

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.
Sponsor
Sponsor
Sponsor
sponsor
Tony




PostPosted: Sat Mar 18, 2006 10:39 pm   Post subject: (No subject)

maincharName wrote:
Same as in the one that worked: take the key

no it's not

code:

var answ : string
get answ
   % "take the key"
put answ
   % "take"

hmm... perhaps you need to read the whole sentance, and not just the first word Wink
code:

var answ : string
get answ:*
put answ

Smile

edit: in a sentance such as "I am keyless" the program will think that you have a key. So just
code:

answ := " " + answ + " "

to add space buffers to beginning and end
Latest from compsci.ca/blog: Tony's programming blog. DWITE - a programming contest.
[Gandalf]




PostPosted: Sat Mar 18, 2006 10:41 pm   Post subject: (No 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.
maincharName




PostPosted: Sat Mar 18, 2006 10:43 pm   Post subject: (No subject)

maincharName wrote:
It works perfectly when the text is predefined:

code:
var answ := "take the key"
if index(answ, "key") > 0 then
  put "you have taken the key"
else
  put "sorry, what?"
end if


But what did I do wrong here?

code:
var answ : string
get answ
if index(answ, "key") > 0 then
  put "you have taken the key"
else
  put "sorry, what?"
end if


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...
code:
var answ : string
get answ

...still doesn't work
Tony




PostPosted: Sat Mar 18, 2006 10:43 pm   Post subject: (No 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 Confused
Latest from compsci.ca/blog: Tony's programming blog. DWITE - a programming contest.
maincharName




PostPosted: Sat Mar 18, 2006 10:45 pm   Post subject: (No subject)

Yes, it was the same! I just neglected to completely paste the code. That problem was in the forum, not the code.
Tony




PostPosted: Sat Mar 18, 2006 10:49 pm   Post subject: (No subject)

maincharName wrote:
That problem was in the forum, not the code.

Unbelivable..
code:

var answ : string
get answ %enter "take the key"
put answ %what does it show?

If you're going to tell me that the value of answ is "take the key", you'd have to upload a screenshot.
Latest from compsci.ca/blog: Tony's programming blog. DWITE - a programming contest.
maincharName




PostPosted: Sat Mar 18, 2006 10:55 pm   Post subject: (No 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.

code:
var answ : string
get answ
if index(answ, "key") > 0 then
put "you have taken the key"
else
put "sorry, what?"
end if


I actually do care and have had turing open the whole time.
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 2  [ 21 Posts ]
Goto page 1, 2  Next
Jump to:   


Style:  
Search: