tags:

views:

4050

answers:

5

Is there a C library function that will return the index of a character in a string?

So far, all I've found are functions like strstr that will return the found char *, not it's location in the original string.

A: 

If you are not totally tied to pure C and can use string.h there is strchr() See here

Adam Haile
I'm not sure what you mean by "pure C" but strchr() and string.h have been part of Standard C for 20 years.
Robert Gamble
+4  A: 

strstr returns a pointer to the found character, so you could use pointer arithmetic: (Note: this code not tested for its ability to compile, it's one step away from pseudocode.)

char * source = "test string";         /* assume source address is */
                                       /* 0x10 for example */
char * found = strstr( source, "in" ); /* should return 0x18 */
if (found != NULL)                     /* strstr returns NULL if item not found */
{
  int index = found - source;          /* index is 8 */
                                       /* source[8] gets you "i" */
}
Bill
Undefined behavior if the character is not found. Also you should not use `strstr` (with or without the typo) if `strchr` would suffice.
R..
@R.. - I fixed the typo and added error checking to the pseudocode. Also, `strchr` does not work for finding multi-character sequences, does it?
Bill
Indeed, but OP asked for a character not a substring. Of course if you interpret character broadly to include multibyte characters, `strstr` is the right function to use.
R..
@R.. - Since the OP was talking about a character as in text, not as in the `char` storage name, I gave a solution that has a hope of working for a character like 輪. You object?
Bill
No objection. :-)
R..
+3  A: 

EDIT: strchr is better only for one char. Pointer aritmetics says "Hellow!":

char *pos = strchr (myString, '#');
int pos = pos ? pos - myString : -1;

Important: strchr () returns NULL if no string is found

Michal Sznajder
did you run out of variable names? ;-)
Isak Savo
0 is a pretty bad return value for `pos` if the character is not found. It's the same as if the character were found at position 0. `strlen(myString)` or `-1` might be better choices.
R..
good idea about this -1 -> fixed
Michal Sznajder
+2  A: 

You can use strstr to accomplish what you want. Example:

char *a = "Hello World!";
char *b = strstr(a, "World");

int position = b - a;

printf("the offset is %i\n", position);

This produces the result:

the offset is 6
Kyle Cronin
UB if there is no match (subtraction of pointers not within the same object).
R..
+4  A: 

I think that

size_t strcspn ( const char * str1, const char * str2 );

is what you want. Here is an example pulled from here:

/* strcspn example */
#include <stdio.h>
#include <string.h>

int main ()
{
  char str[] = "fcba73";
  char keys[] = "1234567890";
  int i;
  i = strcspn (str,keys);
  printf ("The first number in str is at position %d.\n",i+1);
  return 0;
}
Jon Works
`strcspn` is more like `String.IndexOfAny()` - searching for any of the characters in the `keys` array. But yeah, it'll do the trick
Isak Savo