Hi team
I often find myself need to match a string with the strings in a list, what I usually do is just to simply loop through them all. It's OK for a small list, but what if the list a lot bigger, is there some sort of algorithm to speed thing up??
This is how I usually do it.
I often find myself need to match a string with the strings in a list, what I usually do is just to simply loop through them all. It's OK for a small list, but what if the list a lot bigger, is there some sort of algorithm to speed thing up??
This is how I usually do it.
C:
const char *list[] = {
"cmd1",
"cmd2",
"cmd3",
/* more commands */
};
void main(void){
while(1){
/* get user input */
uint8_t buffer[32] = {0};
function_get_user_input(buffer);
/* find a match in the list */
for(unsigned i = 0; i < list_size; i++){
if (strcmp(list[i], buffer) == 0){
/* do stuff */
}
}
}
}