28

Is there a function in c that will return the index of a char in a char array?

For example something like:

char values[] = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
char find = 'E';

int index = findIndexOf( values, find );
Michel Keijzers
  • 15,025
  • 28
  • 93
  • 119
Josh Curren
  • 10,171
  • 17
  • 62
  • 73

7 Answers7

57

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
}
Jesse Beder
  • 33,081
  • 21
  • 109
  • 146
9

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");
John Bode
  • 119,563
  • 19
  • 122
  • 198
5
int index = strchr(values,find)-values;

Note, that if there's no find found, then strchr returns NULL, so index will be negative.

Michael Krelin - hacker
  • 138,757
  • 24
  • 193
  • 173
  • Pointer arithmetic on `NULL` is undefined behavior. It doesn't have to return any particular value. – Konrad Borowski Dec 20 '13 at 08:20
  • That's true, it doesn't have to, but it does usually :). My point was that one should expect NULL pointer here. – Michael Krelin - hacker Dec 20 '13 at 14:05
  • Just because it appears to work for you, it doesn't mean that's right. Your code may randomly fail when pointer is high enough (I actually checked that, see https://gist.github.com/GlitchMr/8056175 (should be an infinite loop, if this would work correctly)). – Konrad Borowski Dec 20 '13 at 15:21
  • I didn't say it's right. And yes, that's very correct what you point out, but it is only because the behaviour is de facto defined you could do that ;) – Michael Krelin - hacker Dec 20 '13 at 18:48
3

Safe index_of() function that works even when it finds nothing (returns -1 in such case).

#include <stddef.h>
#include <string.h>
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;
}
Konrad Borowski
  • 11,584
  • 3
  • 57
  • 71
1

What about strpos?

#include <string.h>

int index;
...
index = strpos(values, find);

Note that strpos expects a zero-terminated string, which means you should add a '\0' at the end. If you can't do that, you're left with a manual loop and search.

Lasse V. Karlsen
  • 380,855
  • 102
  • 628
  • 825
1

You can use strcspn() to get the index of a char in a string, or use my lousy implementation:

// Returns the index of the first occurrence of a char
int string_indexof(char ch, char *everything) {
    int everythingLength = strlen(everything);
    for (int i = 0; i < everythingLength; i++) {
        if (ch == everything[i]) {
            return i;
        }
    }

    return -1;
}
moonasteroid
  • 65
  • 1
  • 5
0

You can use strchr to get a pointer to the first occurrence and the subtract that (if not null) from the original char* to get the position.

Ed S.
  • 122,712
  • 22
  • 185
  • 265