View previous topic :: View next topic |
Author |
Message |
glenjoy
Joined: 18 Oct 2004 Posts: 21
|
Search text from a string? |
Posted: Sun Jan 16, 2011 7:20 am |
|
|
If I have a string say, *string="jdslkajdsldjsladjABCDEFkjdfsljdsalk" and I want to check if ABCDEF do exists in that string, what is the best algorithm to use? |
|
|
dyeatman
Joined: 06 Sep 2003 Posts: 1934 Location: Norman, OK
|
|
Posted: Sun Jan 16, 2011 7:40 am |
|
|
Try looking in the current CCS C Manual under Standard String Functions on page 266.
There you will find a function that searches for one string in another.
For example usage you can simply Google on that function and you will find a lot of examples _________________ Google and Forum Search are some of your best tools!!!! |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19552
|
|
Posted: Sun Jan 16, 2011 10:40 am |
|
|
There is also the question, of 'where is the string coming from'?.
If (for example), the long string is arriving from a source like the serial, then a state machine, may be a much more efficient 'algorithm'
What you do, is test each incoming character as it arrives, against the first character in the 'search' string (state 0). If this is seen, you advance to state one. State one, tests the arriving character against the next required value. If a good match, it moves to state two. If a failure, it moves back to state 0, and tries again. Similarly for each character in the incoming data.
Basically, there is normally only one test for each incoming character, and when the 'state' reaches the number of characters you are looking for, the required pattern has been seen.
'Walking' through a test pattern as data arrives, gives better efficiency in general, but at the cost of complexity. YPYMATYC.
Best Wishes |
|
|
gpsmikey
Joined: 16 Nov 2010 Posts: 588 Location: Kirkland, WA
|
|
Posted: Sun Jan 16, 2011 1:12 pm |
|
|
"YPYMATYC" ???? Google only finds something similar that is a pesticide. Hadn't heard that one before ... YMMV
mikey _________________ mikey
-- you can't have too many gadgets or too much disk space !
old engineering saying: 1+1 = 3 for sufficiently large values of 1 or small values of 3 |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19552
|
|
Posted: Sun Jan 16, 2011 1:57 pm |
|
|
You Pays Your Money And Takes Your Choice/Chance.
Best Wishes |
|
|
gpsmikey
Joined: 16 Nov 2010 Posts: 588 Location: Kirkland, WA
|
|
Posted: Sun Jan 16, 2011 2:14 pm |
|
|
:-) Thanks !!
mikey _________________ mikey
-- you can't have too many gadgets or too much disk space !
old engineering saying: 1+1 = 3 for sufficiently large values of 1 or small values of 3 |
|
|
djsb
Joined: 29 Jan 2011 Posts: 41
|
|
Posted: Sun Jan 30, 2011 7:59 am |
|
|
Hi,
I'm trying to extract \n , \f and \b from a serial character stream sent from a PC to a 16f628A using the USART module. I need to extract the escape sequence's as I'm trying to send clear screen, backspace and newline commands to an LCD using Flex_LCD.C. Can someone expand for me on the state machine algorithm. Any examples I can use to get me started.
I've got the characters echoing to the LCD without problems, just need to be able to send \n etc and have this recognised as a command.
Any help appreciated. Thanks. |
|
|
bkamen
Joined: 07 Jan 2004 Posts: 1615 Location: Central Illinois, USA
|
|
Posted: Sun Jan 30, 2011 9:18 am |
|
|
Ttelmah wrote: | You Pays Your Money And Takes Your Choice/Chance.
|
Should have them add it here:
http://www.cygwin.com/acronyms/ _________________ Dazed and confused? I don't think so. Just "plain lost" will do. :D |
|
|
|