Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Algorithms
  4. Searching string..

Searching string..

Scheduled Pinned Locked Moved Algorithms
algorithmslearning
4 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • Q Offline
    Q Offline
    Quake2Player
    wrote on last edited by
    #1

    Hello! Is there any algorithm for something like this: bool Matches(string text, string search) {..} Which allows me to search in the following way.. Lets say text = "hello how are you blah blah k thanks bye" Searching for "'hello how'" would return true (Notice the ') Searching for "'hello howdy'" would return false Searchinf for "hoy hello are" would return true (Notice there are not ' in this search) Well, you get the idea ( I hope so) Its like google search but, of course simplier

    L F W 3 Replies Last reply
    0
    • Q Quake2Player

      Hello! Is there any algorithm for something like this: bool Matches(string text, string search) {..} Which allows me to search in the following way.. Lets say text = "hello how are you blah blah k thanks bye" Searching for "'hello how'" would return true (Notice the ') Searching for "'hello howdy'" would return false Searchinf for "hoy hello are" would return true (Notice there are not ' in this search) Well, you get the idea ( I hope so) Its like google search but, of course simplier

      L Offline
      L Offline
      Luc Pattyn
      wrote on last edited by
      #2

      Hi, this should do it (pseudo-code):

      stringlist list=new stringlist
      string[] searches=search.split('\'') // split on quotes
      bool quoted=false
      foreach string s in searches {
      if (quoted) list.add(s)
      else foreach string s2 in s.split(' ') list.add(s2) // split on spaces
      quoted=!quoted
      }
      foreach string s in list if !text.contains(s) return false
      return true

      PS: it could be handled without the list, just test right away instead of adding to list... :)

      Luc Pattyn [Forum Guidelines] [My Articles]


      Avoiding unwanted divs (as in "articles needing approval") with the help of this FireFox add-in


      modified on Friday, April 24, 2009 11:38 PM

      1 Reply Last reply
      0
      • Q Quake2Player

        Hello! Is there any algorithm for something like this: bool Matches(string text, string search) {..} Which allows me to search in the following way.. Lets say text = "hello how are you blah blah k thanks bye" Searching for "'hello how'" would return true (Notice the ') Searching for "'hello howdy'" would return false Searchinf for "hoy hello are" would return true (Notice there are not ' in this search) Well, you get the idea ( I hope so) Its like google search but, of course simplier

        F Offline
        F Offline
        Fatbuddha 1
        wrote on last edited by
        #3

        You could also try to solve that with a suffix tree or array approach. This is, up to my knowledge, usually the fastest solution. Cheers

        You have the thought that modern physics just relay on assumptions, that somehow depends on a smile of a cat, which isn’t there.( Albert Einstein)

        1 Reply Last reply
        0
        • Q Quake2Player

          Hello! Is there any algorithm for something like this: bool Matches(string text, string search) {..} Which allows me to search in the following way.. Lets say text = "hello how are you blah blah k thanks bye" Searching for "'hello how'" would return true (Notice the ') Searching for "'hello howdy'" would return false Searchinf for "hoy hello are" would return true (Notice there are not ' in this search) Well, you get the idea ( I hope so) Its like google search but, of course simplier

          W Offline
          W Offline
          wb_program
          wrote on last edited by
          #4

          In c++: #include <string.h> char *strstr( const char *str1, const char *str2 ); The function strstr() returns a pointer to the first occurrence of str2 in str1, or NULL if no match is found. If the length of str2 is zero, then strstr() will simply return str1. For example, the following code checks for the existence of one string within another string: char* str1 = "this is a string of characters"; char* str2 = "a string"; char* result = strstr( str1, str2 ); if( result == NULL ) printf( "Could not find '%s' in '%s'\n", str2, str1 ); else printf( "Found a substring: '%s'\n", result ); :laugh:

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups