Mega Code Archive

 
Categories / C / Code Snippets
 

Search string

#include <stdio.h> #include <string.h> int search(char *p[], char *name); char *names[] = {"Clementine","Clark","Catherine","Superman",NULL}; int main(void) { if(search(names, "Superman") != -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 */ }