Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

searching 10 bytes of char *

Status
Not open for further replies.

jimberger

Programmer
Jul 5, 2001
222
GB
Hi,

I have a char * that can be of any size, lets say for example 100 bytes. I want to be able to seek to the first 10 bytes and compare that with the first 10 bytes of another char * and so on..so next i will move on 10 bytes and compare these 10 bytes with the corresponding char *.

Any ideas on how i can do this? my c knowledge is very limited.
thanks for your help
jim
 
Use memcmp(buf1,buf2,count) from <cstring> (in C++) or <string.h> (in C) to compare bytes. It returns:
< 0 - buf1 count bytes less than buf2
0 - buf1 identical to buf2
> 0 - buf1 greater than buf2
Use buf1 += 10 to advance the pointer.
I don't know what else. I think it's enough info to program your case. Your functional specifications are slightly incomplete...
 
this is not really what you want, (feel free
to make changes) it print starting pos of fully
matching string a in string b.
Code:
#include <stdio.h>
#define MAX 256
#define MIN 32

int main()
{
    char bigstr[MAX];
    char findme[MIN];
    char *big, *find;
    
    strcpy(bigstr,"abc bcabcabcabcabcabc  abc");
    strcpy(findme,"abc");
   
    for(big = bigstr; *big;){

       for(find = findme; *big && *find != *big; ++big);

       for(; *find && *find == *big; ++find, ++big);

       if(*find) continue;

       printf("%s found at pos %d\n",findme, big-bigstr-find-findme);
    }
    exit(0);
}
 
To search a zero-ended substring in a zero-ended string use strstr() library function:
Code:
char* psub = strstr(bigstr,findme);
It's a portable, compact and fast way to go...
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top