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

Username:   Password: 
 RegisterRegister   
 If Statements based on keywords in string variables.
Index -> Programming, Turing -> Turing Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
JamieG




PostPosted: Thu Feb 18, 2016 8:57 pm   Post subject: If Statements based on keywords in string variables.

I was just wondering if I was able to make an if statement based on a keyword from a variable's string. Say I wanted the program to look through the answers and do something to any that contained X word, an example being: "If answer1 = "Keyword" then" would I be able to or is Turing not that advanced? Apologies if this has been answer before, I didn't find it in the search and sorry about my English, it's not my first language!
Sponsor
Sponsor
Sponsor
sponsor
Nathan4102




PostPosted: Thu Feb 18, 2016 11:35 pm   Post subject: RE:If Statements based on keywords in string variables.

Before you try to find if string X ID contained in a list of strings try just one first. The problem you're trying to solve is figuring out of X is a substring (subset) of Y. You might be able to search the docs and find an existing function, or you could write your own. It's a tricky problem for a beginner, so you might want to look up some examples in other languages to figure it out if you're writing your own.
JamieG




PostPosted: Fri Feb 19, 2016 1:21 am   Post subject: Re: RE:If Statements based on keywords in string variables.

Nathan4102 @ February 18th 2016, 11:35 pm wrote:
Before you try to find if string X ID contained in a list of strings try just one first. The problem you're trying to solve is figuring out of X is a substring (subset) of Y. You might be able to search the docs and find an existing function, or you could write your own. It's a tricky problem for a beginner, so you might want to look up some examples in other languages to figure it out if you're writing your own.


I'm not sure if I understood what you meant about if X is a substring of Y. I am specifically looking to see if there is a function or a method where I can take a variable that is XYZ and make an if statement saying that if that variable has an X in it, it will do a put a message. As for substrings, as I understand it they only deal with characters while I am looking to single out certaine words in multi world responses string variables.
Nathan4102




PostPosted: Fri Feb 19, 2016 9:01 am   Post subject: RE:If Statements based on keywords in string variables.

Ah OK, so if I understand correctly:

You have a strong "Hello my name is Amy", and you want to return all words in the string containing some string, say "my", which would return " my" and "Amy" and do sometging with those? If I've still not got it, it might be good to give an example. It's tricky to explain this stuff sometimes.
JamieG




PostPosted: Fri Feb 19, 2016 1:17 pm   Post subject: Re: RE:If Statements based on keywords in string variables.

Nathan4102 @ February 19th 2016, 9:01 am wrote:
Ah OK, so if I understand correctly:

You have a strong "Hello my name is Amy", and you want to return all words in the string containing some string, say "my", which would return " my" and "Amy" and do sometging with those? If I've still not got it, it might be good to give an example. It's tricky to explain this stuff sometimes.


Not quite sure you get it, here's an example. The program asks "how's your day?" and person would input Good, Thanks!" or "Bad" or "Fine." Instead of specifying that "good" and "good thanks" would return the same put, could I make it so that if the person says "Good" anywhere in their response that it outputs the same put statement?
Insectoid




PostPosted: Fri Feb 19, 2016 6:59 pm   Post subject: RE:If Statements based on keywords in string variables.

Sure. Write a function that searches the string for 'good'. If it's there, return true. If not, return false.

But what if the person inputs 'not good'?
JamieG




PostPosted: Fri Feb 19, 2016 8:02 pm   Post subject: Re: RE:If Statements based on keywords in string variables.

Insectoid @ February 19th 2016, 6:59 pm wrote:
Sure. Write a function that searches the string for 'good'. If it's there, return true. If not, return false.

But what if the person inputs 'not good'?


Do you know where I can find a tutorial on how to do this?
Nathan4102




PostPosted: Sat Feb 20, 2016 3:54 pm   Post subject: RE:If Statements based on keywords in string variables.

There's a few different ways to write this function, but I'll lay out an algorithm for you in English and you can try converting to Turing.

First, an example.

Suppose we're looking for "not" in "Imnotgreen". We know our search term is 3 characters, so lets attempt all 3 character substrings of "Imnotgreen".

We'll start with "Imn", the first 3 characters. This isn't "not", so we move one to the right, "mno". Still no good, so next we try "not" which is our search term! We return true. If we made it through the entire string and didn't find "not", we'd return false.

To get substrings of your larger string, look into this function: http://compsci.ca/holtsoft/doc/substring.html

You'll need to be a bit clever with your for loop to avoid errors while using substring (e.g. letting your second argument go higher than the string length). Give it a try, and if it's too hard we can give more hints.
Sponsor
Sponsor
Sponsor
sponsor
JamieG




PostPosted: Mon Feb 22, 2016 8:37 am   Post subject: Re: If Statements based on keywords in string variables.

The only real problems I'm having with the code is in the sub-string. I'm not sure how to make it look through all possible all possible 4 character combinations in the string.
Insectoid




PostPosted: Mon Feb 22, 2016 12:27 pm   Post subject: RE:If Statements based on keywords in string variables.

Is it all possible 4 character combinations, or all 4-character sequences? That's an important distinction.

If you just want sequences, all you need is a for loop. If you want all combinations, then it gets a lot more complicated.
JamieG




PostPosted: Mon Feb 22, 2016 12:30 pm   Post subject: RE:If Statements based on keywords in string variables.

4 Letter sequences within the string.
Nathan4102




PostPosted: Tue Feb 23, 2016 9:46 pm   Post subject: RE:If Statements based on keywords in string variables.

Ok, I'll try to walk you through the thought process you should be having when solving problems like this.

So we know we're working with some sort of pattern, we're beginning at the left of the string and moving one subsequence to the right until we hit the right side of the string.

Let's try a couple examples and see if we notice any explicit patterns that we might be able to mimic in a for-loop.

I'm going to refer to "imnotgreen" as STRING, and "not" as SEARCH. Same as my last example, We're searching for SEARCH in STRING.

So we want to start with the first 3 letters of STRING, "imn". Playing around with substring, we notice this is equal to STRING (1 .. 3). Is STRING (1 .. 3) = SEARCH? No, STRING (1 .. 3) is "imn", SEARCH is "not".

Now we move one to the right. Again, by playing around with substring, we learn this is STRING (2 .. 4). Is STRING (2 .. 4) = SEARCH? Nope, STRING (2 .. 4) is "mno".

By this point, you should notice that to move the substring to the right, we increment each argument by one. By this rule, the next substring is STRING (3 .. 5). This substring is equal to our search term, so we're finished.

Some observations:

- We always begin with STRING (1 .. X) where X is the length of our search string.

- In each iteration, we increment both arguments by 1. Perhaps we could use variables for these numbers and increment them in a for loop?

- We'll need to stop the loop when we reach the end of the string. That is, we should stop if incrementing X would make it > STRING length.

From this information, see if you can put together a working version of the algorithm. Smile
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 1  [ 12 Posts ]
Jump to:   


Style:  
Search: