26

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
  • 14,510
  • 27
  • 88
  • 115
Josh Curren
  • 9,741
  • 17
  • 60
  • 72

7 Answers7

56

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
  • 31,716
  • 20
  • 105
  • 143
8

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
  • 113,266
  • 18
  • 112
  • 190
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
  • 131,515
  • 23
  • 189
  • 171
  • 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
  • 10,745
  • 3
  • 55
  • 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
  • 366,661
  • 96
  • 610
  • 798
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.
  • 119,398
  • 20
  • 176
  • 254