finding sub_array in array

For C and ASSEMBLY users to post questions and code snippets for programming in C and ASSEMBLY. And for any other C or ASM course related questions.

Moderators: Benj, Mods

Post Reply
Eric
Posts: 99
Joined: Tue Nov 06, 2007 11:04 pm
Been thanked: 1 time

finding sub_array in array

Post by Eric »

Hello,

I need to verify the occurence of a 8 byte sub-array in another 130 bytes array.
I can't use the C-function strstr because there are zero bytes in both arrays.
Can anybody show me the way how to do this?

Best regards,

Eric

User avatar
Benj
Matrix Staff
Posts: 15312
Joined: Mon Oct 16, 2006 10:48 am
Location: Matrix TS Ltd
Has thanked: 4803 times
Been thanked: 4314 times
Contact:

Re: finding sub_array in array

Post by Benj »

Hello Eric,

Its best to do this kind of thing byte by byte.

Eg you start a counter variable at 0 the start of the array. Then move through the array until you find the first character that matches the first character of your string. Then move to the second character if this does not match the second character in your string then does it match the first. And so on until you have found the entire string.

idx = 0
found = 0
character = 0
while found = 0
{
if string[idx] = compare[character]
yes: character = character + 1
no: character = 0

if character = length of search string
yes: found = 1

idx = idx + 1
}

Eric
Posts: 99
Joined: Tue Nov 06, 2007 11:04 pm
Been thanked: 1 time

Re: finding sub_array in array

Post by Eric »

Thanks Ben, I will give it a try.

Rgds,

Eric

Post Reply