1、线性查找法
#include <stdio.h> #define FAILED -1 int len(char x[]) { int len = 0; while(x[len]) len++; return len; } int search(char x[], char key) { int i = 0; while(1) { if(i == len(x)) return FAILED; if(x[i] == key) return i; i++; } } int main(void) { char str[128]; printf("str: "); scanf("%s", str); printf("position: %d ", search(str, 'c')); return 0; }
2、线性查找法
#include <stdio.h> #define FAILED -1 int len(char x[]) { int i = 0; while(1) { if(x[i] == '