Is there a function in c that will return the index of a char in a char array?
For example something like:
char values[] = \"0123456789ABCDEFGHIJKLMN
Safe index_of()
function that works even when it finds nothing (returns -1
in such case).
#include
#include
ptrdiff_t index_of(const char *string, char search) {
const char *moved_string = strchr(string, search);
/* If not null, return the difference. */
if (moved_string) {
return moved_string - string;
}
/* Character not found. */
return -1;
}