Search of string array isn't working correctly
Hi all -
I've written this code to search an array of strings for a given string. Here it is -
Code:
/* search.c */
/* Function to search an array of strings */
/* to find a given string. */
/* This code is released to the public domain. */
/* "Share and enjoy...." :) */
#include <stdio.h>
#include <ctype.h>
#include <string.h>
#include <stdlib.h>
/* Array of strings. */
static const char *kw_strings[] = {
"select", "from", "where", "and", "or", "not", "in", "is", "null"
} ;
/* Search function. */
void search(const char *arr[], char *str) {
int len = sizeof(arr) / sizeof(arr[0]) ;
int i;
for (i=0; i<len; i++) {
if ( !strcmp(arr[i] , str ) ) {
printf("%s \n", "Found");
}
else {
printf("%s \n", "Not Found");
}
} /* For */
} /* search */
int main() {
search(kw_strings , "select") ;
search(kw_strings , "from") ;
search(kw_strings , "foobar") ;
search(kw_strings , "I like moose") ;
return 0;
}
This *should* result in the first two searches succeeding.
However, I'm getting the first one succeeding and then three fails. Any help on why this is is very much appreciated!
- Andy ( latte123 )