#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define NUM(a) (sizeof(a)/sizeof(*a))
int searchKeyTable(const char* table[], const int size,
const char* key, int* pos)
{
if (table == NULL || key == NULL || pos == NULL)
{
return -1;
}
int n = -1;
for (int i = 0; i < size; i++)
{
if (strcmp(table[i], key) == 0)
{
n = i;
break;
}
}
if (n == -1)
{
printf("无此字符串\n");
return -2;
}
*pos = n + 1;
return 0;
}
int main()
{
char* keywords[] = {
"while",
"case",
"static",
"do"
};
int ret = 0;
int size = NUM(keywords);
int pos = 0;
char* key = "do";
ret = searchKeyTable(keywords, size, key, &pos);
if (ret != 0)
{
printf("searchKeyTable error:%d\n", ret);
return ret;
}
printf("'%s'的位置是:%d\n", key, pos);
printf("\n");
system("pause");
return 0;
}
int fun(int x, int s[]) {
int index = -1;
int size = sizeof(s) / sizeof(s[0]); // 数组个数
for (int i = 0; i < size; i++) {
if (s[i] == x) {
index = i;
break;
}
}
return index;
}