c中是否有一个函数可以返回char数组中char的索引? [英] Is there a function in c that will return the index of a char in a char array?
本文介绍了c中是否有一个函数可以返回char数组中char的索引?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
c 中有一个函数可以返回 char 数组中 char 的索引吗?
Is there a function in c that will return the index of a char in a char array?
例如:
char values[] = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
char find = 'E';
int index = findIndexOf( values, find );
推荐答案
strchr
返回指向第一次出现的指针,所以要找到索引,只需取起始指针的偏移量即可.例如:
strchr
returns the pointer to the first occurrence, so to find the index, just take the offset with the starting pointer. For example:
char values[] = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
char find = 'E';
const char *ptr = strchr(values, find);
if(ptr) {
int index = ptr - values;
// do something
}
这篇关于c中是否有一个函数可以返回char数组中char的索引?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文