Computer Science Canada

Check if a string contains a word/sentence etc in Turing

Author:  ThgilFoDrol [ Fri Nov 02, 2012 10:45 am ]
Post subject:  Check if a string contains a word/sentence etc in Turing

How do I check if a "get x" has a specific word/sentence in Turing? Is it possible in Turing? In java it's myString.contains(string[, separator]); how how would I do this in Turing? Thanks in advance.

Author:  Aange10 [ Fri Nov 02, 2012 11:54 am ]
Post subject:  RE:Check if a string contains a word/sentence etc in Turing

You can do it, you just have to make the .contains procedure.

It would look something like

code:
fcn contains (word, lookingFor : string) : boolean
     for i : 1 .. length(word)
     if word (i) = lookingFor then
        result true
     end if
end for
result false
end contains

var s : string := "Hello World"
put contains (s, "o")

Author:  Tony [ Fri Nov 02, 2012 12:19 pm ]
Post subject:  RE:Check if a string contains a word/sentence etc in Turing

index

Author:  Aange10 [ Fri Nov 02, 2012 12:44 pm ]
Post subject:  RE:Check if a string contains a word/sentence etc in Turing

Oh yes, I forgot about index. But the Turing index function is terrible considering it stops at the first occurrence of what you are looking for, and doesn't provide a way to look for more occurrences.

That would be why I do not use index.

Author:  Tony [ Fri Nov 02, 2012 3:54 pm ]
Post subject:  Re: RE:Check if a string contains a word/sentence etc in Turing

Aange10 @ Fri Nov 02, 2012 12:44 pm wrote:
it stops at the first occurrence of what you are looking for, and doesn't provide a way to look for more occurrences.

Considering that #contains() returns a boolean value, I don't see how this is a problem.

Author:  Aange10 [ Fri Nov 02, 2012 9:58 pm ]
Post subject:  Re: RE:Check if a string contains a word/sentence etc in Turing

Not to OT, but:

Tony @ 2/11/2012, 2:54 pm wrote:
Aange10 @ Fri Nov 02, 2012 12:44 pm wrote:
it stops at the first occurrence of what you are looking for, and doesn't provide a way to look for more occurrences.

Considering that #contains() returns a boolean value, I don't see how this is a problem.


->

Aange10 wrote:

That would be why I do not use index.


Not saying it matters in this scenario (or in comparison to #contains() in java)

On the flip side, to the OP, Tony is correct about index being a valid and most likely superior way to solve your problem.


: