My program is accepting user input and then taking the first word inputted and comparing it to an array of accepted commands. What would be the best way to compare the first word inputted (after it has been tokenized) to an array of strings?
Example:
comparing the string "pwd"
to an array containging {"wait", "pwd", "cd", "exit"}
Thanks in advance for your help!
I would do something like the following:
int string_in(const char* string, const char** strings, size_t strings_num) {
for (size_t i = 0; i < strings_num; i++) {
if (!strcmp(string, strings[i])) {
return i;
}
}
return -1;
}
Check each string in the array, if it's the same return the index. Return -1
if not found.
Note: Vulnerable to overflows, etc, fix them before trying to use this code. This will give you an idea of what to do, but is not good code.