Search string
#include <stdio.h>
#include <string.h>
int search(char *p[], char *name);
char *names[] = {"John","Jim","Jane","James",NULL};
int main(void)
{
if(search(names, "James") != -1)
printf("in list.\n");
if(search(names, "Bill") == -1)
printf("not found.\n");
return 0;
}
int search(char *p[], char *name)
{
int t;
for(t=0; p[t]; ++t){
if(!strcmp(p[t], name)) {
return t;
}
}
return -1; /* not found */
}
Related examples in the same category