indexOf equivalent for c-string ?

As i am working on a project that needs to check responses of the SIM800l to AT-commands, i easily want to be able to check for keywords within a c-string. I am using a Mega, which does have quite a lot of memory but it's not infinite, and i don't really want to use the 'String' class because of that. Still the String.indexOf() does exactly what i want it to do, but after some googling i did not find anything that i could just use as a near 1 on 1 swap. Then of course i figure "Why look for something that can easily be created ?" so i came to this

int indexOf(const char* source, const char * fnd) {
  int ndx = 0;
  while (source[ndx]) {
    int cmpndx = 0;
    while ((source[ndx+cmpndx]) && (source[ndx+cmpndx] == fnd[cmpndx])) {
      cmpndx++;
      if (!fnd[cmpndx]) return ndx;
    }
    ndx++;
  }
  return -1;
}

I was just wondering if there actually is something already, and looked at strncmp() but that doesn't return the value that i want.

strstr() for a string
strchr() for a character

Both return a pointer, not a position. If needed, you can use pointer arithmetic to get the position.

If needed, you can use pointer arithmetic to get the position.

Yes that would work !
Though the issue for me remains that these functions use the same 'building-blocks' that i use, but not return the type i want them to. I would still need to create an 'adapter'
Thank you for this answer.

Question is if you really need the position :wink:

sterretje:
Question is if you really need the position :wink:

No i guess for nowe i probably don't. Though i will need it when i receive a call (for the caller ID) and when i receive SMS i will as well.

Deva_Rishi:
Though the issue for me remains that these functions use the same 'building-blocks' that i use, but not return the type i want them to. I would still need to create an 'adapter'

Not sure what your complaint is. It could also be said that "indexOf() returns an index and I want a pointer". They're different functions.

Deva_Rishi:
No i guess for nowe i probably don't. Though i will need it when i receive a call (for the caller ID) and when i receive SMS i will as well.

Just give an example string and we will probably find you a way :wink: