简体   繁体   中英

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;

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;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM