MD XF - 6 months ago 35

C Question

I saw this question but none of the answers were quite what I was looking for. I've tried

`strstr`

I need to find if string

`a`

`b`

`strcmp`

For example, if

`a`

`b`

Any help is appreciated!

Answer

You can use `strstr`

for this, along with some pointer arithmetic.

```
char *result = strstr(a, b);
if (result != NULL) {
printf("index = %tu\n", result - a);
}
```

Here, `result`

points a particular number of bytes ahead of `a`

. So if you subtract the two, that's your index.