'Search through part of a string
I am trying to search a string from the middle from index i and get the index of the first instance found, and then change what is stored in the string at this point. I know I can use strstr to search through the string, but this starts at the start of a string. How can I start searching in the middle of the string?
int i=5;
char str[]="mystringismystring";
char *pos;
pos=strstr(str, "my");
int index=pos-str;
Solution 1:[1]
If you always have information about data length you can point to the pointer from where you want like &str[strlen(str) / 0x2] or you can put location an array instead of strlen(str).
Code:
char str[] = "mystringismystring";
char *pos;
pos = strstr(&str[strlen(str) / 0x2], "my");
int index = pos - str;
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | 936 |
