C语言 indexOf
#include <stdio.h>
//str为字符串
//ch为你所需要匹配的字符
int indexOf(char* str, char ch){
int index = 0;
char tmp=str[index];
// 当这个字符还不是最后一个字符时做判断
while( tmp != ‘\0’){
if(tmp == ch){
return index;
}
index++;
tmp = str[index];
}
//找不到则返回 -1
return -1;
}
int main(){
printf("%d",indexOf(“sdda”,‘m’));
}
比如查询str字符串中a的位置:str.indexOf(a); 就可以了
如果str中没有a,则返回-1