mijmajmoj
mijmajmoj

Reputation: 59

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;

Upvotes: 0

Views: 100

Answers (1)

936
936

Reputation: 11

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;

Upvotes: 1

Related Questions