如题,空间换时间查找字符是否存在某字符串里的例子,下面的代码里字符串长度比较短,不能体现优化的时间,如果比较长的字符串,它的优势就很明显了。总之,抛砖引玉吧。
#include <stdio.h>
#include <string.h>
const char str[] = "abcdef";
int main()
{
int str_table[256]={0}, len, i;
char ch = 'g';
len = strlen(str);
for ( i=0; i < len; i++)
str_table[str[i]] = 1;
if (ch & str_table[ch])
printf("found char\n");
else
printf("not found char\n");
}