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

7

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 );
Lowe answered 25/9, 2009 at 20:19 Comment(2)
strchr()Balkin
possible duplicate of String.indexOf function in CFarina
V
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
}
Voltameter answered 25/9, 2009 at 20:22 Comment(3)
my question, how do I find indexes in a char. If I had 2 E s in the string?Sartre
You did not tell me what to #includeChally
@turmuka then it will only return the first occuranceSandler
T
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");
Tybi answered 25/9, 2009 at 20:29 Comment(0)
M
5
int index = strchr(values,find)-values;

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

Mangle answered 25/9, 2009 at 20:23 Comment(4)
Pointer arithmetic on NULL is undefined behavior. It doesn't have to return any particular value.Inbreeding
That's true, it doesn't have to, but it does usually :). My point was that one should expect NULL pointer here.Mangle
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 gist.github.com/GlitchMr/8056175 (should be an infinite loop, if this would work correctly)).Inbreeding
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 ;)Mangle
I
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;
}
Inbreeding answered 20/12, 2013 at 15:36 Comment(0)
B
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.

Bluey answered 25/9, 2009 at 20:22 Comment(7)
I don't think that exists in the C standard library, though I could be wrong.Dermatome
Personally I have more confidence in the strchr answers, I'd really like one of those be marked as the answer instead. I only found one really old piece of source code using that function, and I don't know how standard it is when I consider it.Bluey
strpos() is not a standard C (or even C++) function. Quite common in PHP, etc...Margetmargette
Odd, but I do have a piece of C code that uses it. But as I said, I don't know how standard it is.Bluey
strpos isn't part of the C std lib - but it is probably present on most systems.Catholicon
after looking through some other code I have found some c that uses strpos()Lowe
+1 for reminding that we can do the manual loop. Cuz in my case i need something like lfind not the rfind which strchr will return.Sexism
H
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;
}
Hexateuch answered 30/6, 2021 at 6:45 Comment(0)
D
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.

Dermatome answered 25/9, 2009 at 20:25 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.