array iteration strstr in c

Posted by lex0273 on Stack Overflow See other posts from Stack Overflow or by lex0273
Published on 2010-03-29T23:36:29Z Indexed on 2010/03/29 23:43 UTC
Read the original article Hit count: 379

Filed under:
|
|
|

I was wondering if it's safe to do the following iteration to find the first occurrence of str within the array or if there is a better way. Thanks

#include <stdio.h> 
#include <string.h>

const char * list[] = {"One","Two","Three","Four","Five"};

char *c(char * str) {
    int i;
    for (i = 0; i < 5; i++) {
        if (strstr(str, list[i]) != NULL) return list[i];
    }
    return "Not Found";
}
int main() {
    char str[] = "This is a simple string of hshhs wo a char";

    printf("%s", c(str));
    return 0;
}

© Stack Overflow or respective owner

Related posts about c

    Related posts about iteration