54

Suppose I have a string "qwerty" and I wish to find the index position of the e character in it. (In this case the index would be 2)

How do I do it in C?

I found the strchr function but it returns a pointer to a character and not the index.

Mateusz Piotrowski
  • 8,029
  • 10
  • 53
  • 79
bodacydo
  • 75,521
  • 93
  • 229
  • 319

5 Answers5

107

Just subtract the string address from what strchr returns:

char *string = "qwerty";
char *e;
int index;

e = strchr(string, 'e');
index = (int)(e - string);

Note that the result is zero based, so in above example it will be 2.

Erikas
  • 1,006
  • 11
  • 22
wj32
  • 8,053
  • 3
  • 28
  • 37
  • 1
    This is "right" but theoretically scary in that there's nothing in the standard stopping the length of a string from vastly exceeding the capacity of a plain 'int'. If you can depend on C99, use uintptr_t (defined in stdint.h) for this. It should also eliminate the need for that cast. – Nicholas Knight Jul 10 '10 at 03:43
  • 11
    Yes, that's true. However I thought at least for a beginner example an int would be much clearer, and I am certain that the OP is not dealing with 5 GB strings. – wj32 Jul 10 '10 at 05:28
  • 9
    `size_t` is probably the clearest one to use, since it's big enough to hold the length of any object (and thus any string). The *real* issue with this code is that you should have `if (e) { } else { }` to handle the case when the character is not found within the string. – caf Jul 10 '10 at 05:36
  • 1
    @wj32: Just for beginners it is important to learn to use the correct types. Most of the times (99%, I guess) `int` is wrong, and all these *beginner* examples just teach them the wrong attitude. – Jens Gustedt Jul 10 '10 at 07:38
  • This solution helps me but as per my findings, in strchr(string, 'e') , 'e' should be in double quotes ("e") , otherwise it throws compile time error. +1 for solution. – iLearner Aug 12 '15 at 12:43
  • as a newbie i would like to ask , what does (int) in index = (int)(e - string);? – Darlyn Oct 22 '15 at 18:02
  • 3
    @iLearner: it is very interesting why your implementation of strchr demands char* as the second parameter. What `C` compiler do you use? – cha Jun 29 '16 at 12:31
8

You can also use strcspn(string, "e") but this may be much slower since it's able to handle searching for multiple possible characters. Using strchr and subtracting the pointer is the best way.

R.. GitHub STOP HELPING ICE
  • 208,859
  • 35
  • 376
  • 711
5
void myFunc(char* str, char c)
{
    char* ptr;
    int index;

    ptr = strchr(str, c);
    if (ptr == NULL)
    {
        printf("Character not found\n");
        return;
    }

    index = ptr - str;

    printf("The index is %d\n", index);
    ASSERT(str[index] == c);  // Verify that the character at index is the one we want.
}

This code is currently untested, but it demonstrates the proper concept.

abelenky
  • 63,815
  • 23
  • 109
  • 159
3

This should do it:

//Returns the index of the first occurence of char c in char* string. If not found -1 is returned.
int get_index(char* string, char c) {
    char *e = strchr(string, c);
    if (e == NULL) {
        return -1;
    }
    return (int)(e - string);
}
Varun Narayanan
  • 343
  • 6
  • 17
1

What about:

char *string = "qwerty";
char *e = string;
int idx = 0;
while (*e++ != 'e') idx++;

copying to e to preserve the original string, I suppose if you don't care you could just operate over *string

Dan Molik
  • 11
  • 2