Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a multiple character version of strchr() in the standard C libraries?

Tags:

c

string

parsing

In c if I wanted to search a string for a particular character I can just do the following

char *p;
p = (char *)strchr(buffer,'(');
if(p){
    ....

but what if I want to search for more than one character (or a character range) for example "any digit". I know I could do something like

char *p=0;
char *i;
for(i=buffer;*i!='\0';i++){
    if(*i >= '0' && *i <=9){
        p=i;
    }
}

if(p){
    ...

assuming a properly terminated string etc. But is there a standard library function that already provides this functionality (hopefully more safely)

like image 382
Vagnerr Avatar asked Jul 15 '10 16:07

Vagnerr


1 Answers

What about strpbrk? Sounds like it fits your bill.

const char * strpbrk ( const char * str1, const char * str2 );

"Returns a pointer to the first occurrence in str1 of any of the characters that are part of str2, or a null pointer if there are no matches."

EDIT: Link is to a C++ site, but strpbrk is part of the standard C library.

like image 145
Greg S Avatar answered Sep 18 '22 01:09

Greg S