How do I check if a string is within another string?

1 favourites
  • 4 posts
From the Asset Store
This collection of music is the beautiful performance of rather small strings ensemble (8 first violins, 6 second violin
  • I'm trying to make a game based around the infinite monkeys on infinite typewriters thought experiment.

    I have a) a string that continuously adds random letters and b) a plain text file containing the entire works of Shakespeare. The idea is that when the random string generates a word that appears in the Works, it's removed from the text file and a variable increases to convey progress.

    I've tried various methods but can't figure out a way that works. Is there an obvious function or different approach that I'm missing?

  • Try Construct 3

    Develop games in your browser. Powerful, performant & highly capable.

    Try Now Construct 3 users don't see these ads
  • Does the string has to be a perfect match or just have to contain the word between the string of letters?

    Example:

    We have Word = Apple

    1-String: asqpApplepd

    2-String: Apple

    Which one is the correct one 1 or 2?

    As it will change the way you will build the logic.

  • Oof..

    I would suggest parsing the entire works of Shakespeare into a list of unique words first. This task itself is going to be tricky. You need to remove all punctuation, line breaks and other non-letter characters, replace them with spaces. Then extract every word using tokencount/tokenat. I suggest writing these words to a dictionary as keys, this is the easiest way to get rid of duplicates.

    Then you can start generating that continuous string, and after adding each letter loop through all dictionary keys and check if the string contain such key.

    For Each key in Dictionary
    find(LongString, Dictionary.currentKey)>=0
    ... // word found!
    ... Dictionary delete Dictionary.currentKey
    

    You'll need to use expressions like tokenat, tokencount, find, trim, replace etc. See this link:

    construct.net/en/make-games/manuals/construct-3/system-reference/system-expressions

    Oh, and the string doesn't need to be endless. If no words were found, you can safely trim the string, leaving only the last 20 or so characters. This should help with the performance.

  • You probably could do it with just the text using find(), but since you want to remove words once they are found it could get slow to replace stuff. We want to do it in a reasonably efficient way. Like dop said, it's useful to first parse the words out of the text and add them into a dictionary. You can even store the number of times each word occurs in the dictionary.

    1. parse the text to get all the words. Each word and the number of times that word is seen is added to a dictionary. This can be done over multiple frames.

    2. Random letters are typed. We only need to keep track of no more than the length of the longest word in characters.

    3. Then just use right(keys, 1), right(keys, 2), right(keys, 3), ...etc. to get a possible word and see if it's in a dictionary.

    dropbox.com/s/cq7yq79mpe8w6n8/monkey_type.capx

    Seems words longer than four letters long are seldom found, but that lines up with the probability.

Jump to:
Active Users
There are 1 visitors browsing this topic (0 users and 1 guests)