array iteration strstr in c
- by lex0273
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;
}