c语言中indexOf()的用法

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

发布了75 篇原创文章 · 获赞 113 · 访问量 5万+

猜你喜欢

转载自blog.csdn.net/xurQQ/article/details/86564865