Is there a function in c that will return the index of a char in a char array?
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.
Related videos on Youtube
Josh Curren
I currently am the Sales & 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, 2022Comments
-
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 );
-
chaos over 14 years
-
Ciro Santilli OurBigBook.com almost 9 yearspossible duplicate of String.indexOf function in C
-
-
Ed S. over 14 yearsI don't think that exists in the C standard library, though I could be wrong.
-
Lasse V. Karlsen over 14 yearsPersonally 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 over 14 yearsstrpos() is not a standard C (or even C++) function. Quite common in PHP, etc...
-
Lasse V. Karlsen over 14 yearsOdd, 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 over 14 yearsstrpos isn't part of the C std lib - but it is probably present on most systems.
-
Josh Curren over 14 yearsafter looking through some other code I have found some c that uses strpos()
-
Konrad Borowski over 10 yearsPointer arithmetic on
NULL
is undefined behavior. It doesn't have to return any particular value. -
Michael Krelin - hacker over 10 yearsThat's true, it doesn't have to, but it does usually :). My point was that one should expect NULL pointer here.
-
Konrad Borowski over 10 yearsJust 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 over 10 yearsI 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 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 over 6 yearsmy question, how do I find indexes in a char. If I had 2
E
s in the string? -
ctrl-alt-delor over 4 yearsYou did not tell me what to
#include
-
Ratul Hasan over 3 years@turmuka then it will only return the first occurance