Is there a function in c that will return the index of a char in a char array?
There's also size_t strcspn(const char *str, const char *set)
; it returns the index of the first occurence of the character in s
that is included in set
:
size_t index = strcspn(values, "E");
int index = strchr(values,find)-values;
Note, that if there's no find
found, then strchr
returns NULL
, so index will be negative.
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
}