How to find a binary string in binary string

Programming, for all ages and all languages.
Post Reply
junkoi
Member
Member
Posts: 63
Joined: Wed Jan 23, 2008 8:55 pm

How to find a binary string in binary string

Post by junkoi »

Hi,

I am programming in C, and I want to find (the position of) a binary string in another binary string. Normally strstr() can be used, but it fails here because it can only use with a null-terminated-string.

So is there any available C function/library for this? Or we can find some implementations doing this somewhere?

(I am on Linux, using gcc and glibc)

Thanks,
Jun
User avatar
Solar
Member
Member
Posts: 7615
Joined: Thu Nov 16, 2006 12:01 pm
Location: Germany
Contact:

Re: How to find a binary string in binary string

Post by Solar »

No, you'll have to code it yourself. I'd start with memchr() looking for the first charakter of string 1 in string 2, aborting if the character is not found or so far back in string 2 that a complete match is no longer possible, and if everything is OK, run memcmp() on the returned address. That's assuming you have some way of determining the size of your binary strings...
Every good solution is obvious once you've found it.
Hyperdrive
Member
Member
Posts: 93
Joined: Mon Nov 24, 2008 9:13 am

Re: How to find a binary string in binary string

Post by Hyperdrive »

junkoi wrote:I am programming in C, and I want to find (the position of) a binary string in another binary string. Normally strstr() can be used, but it fails here because it can only use with a null-terminated-string.

So is there any available C function/library for this? Or we can find some implementations doing this somewhere?
I don't know if there is some C library containing the functionality you ask for. But there are many string matching algorithms out there. A famous one is Knuth-Morris-Pratt. Maybe that helps you to get started.

--TS
Post Reply