I have char * source, and I want extract from it subsrting, that I know is beginning from symbols "abc", and ends where source ends. With strstr I can get the poiner, but not the position, and without position I don't know the length of the substring. How can I get the index of the substring in pure C?
int indexOf(String str, int strt) : This method returns the index within this string of the first occurrence of the specified substring, starting at the specified index.
IndexOf(String substring, int startindex) This Java substring indexOf() method returns the index of the first character in the substring passed as the first parameter, after the “startindex” index value.
The indexOf() method returns the position of the first occurrence of a value in a string. The indexOf() method returns -1 if the value is not found. The indexOf() method is case sensitive.
Use pointer subtraction.
char *str = "sdfadabcGGGGGGGGG"; char *result = strstr(str, "abc"); int position = result - str; int substringLength = strlen(str) - position;
newptr - source
will give you the offset.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With