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

81,709

Solution 1

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
}

Solution 2

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");

Solution 3

int index = strchr(values,find)-values;

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

Solution 4

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;
}

Solution 5

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.

Share:
81,709

Related videos on Youtube

Josh Curren
Author by

Josh Curren

I currently am the Sales &amp; Parts Manager at Curren RV Center. I do website and web app development on the side. I am a graduate of Bloomsburg University of PA with a BA in Theatre Design (emphasis on Lighting Design) and a minor in Computer Science. I was a double major in Computer Science and Theatre but had to give up something to graduate in a reasonable amount of time. I started programming when I was about 14.

Updated on July 09, 2022

Comments

  • Josh Curren
    Josh Curren almost 2 years

    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 );
    
  • Ed S.
    Ed S. over 14 years
    I don't think that exists in the C standard library, though I could be wrong.
  • Lasse V. Karlsen
    Lasse V. Karlsen over 14 years
    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.
  • jesup
    jesup over 14 years
    strpos() is not a standard C (or even C++) function. Quite common in PHP, etc...
  • Lasse V. Karlsen
    Lasse V. Karlsen over 14 years
    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.
  • Martin Beckett
    Martin Beckett over 14 years
    strpos isn't part of the C std lib - but it is probably present on most systems.
  • Josh Curren
    Josh Curren over 14 years
    after looking through some other code I have found some c that uses strpos()
  • Konrad Borowski
    Konrad Borowski over 10 years
    Pointer arithmetic on NULL is undefined behavior. It doesn't have to return any particular value.
  • Michael Krelin - hacker
    Michael Krelin - hacker over 10 years
    That's true, it doesn't have to, but it does usually :). My point was that one should expect NULL pointer here.
  • Konrad Borowski
    Konrad Borowski over 10 years
    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)).
  • Michael Krelin - hacker
    Michael Krelin - hacker over 10 years
    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 ;)
  • Jumabek Alikhanov
    Jumabek Alikhanov about 7 years
    +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.
  • turmuka
    turmuka over 6 years
    my question, how do I find indexes in a char. If I had 2 E s in the string?
  • ctrl-alt-delor
    ctrl-alt-delor over 4 years
    You did not tell me what to #include
  • Ratul Hasan
    Ratul Hasan over 3 years
    @turmuka then it will only return the first occurance