i'm trying to find the position of substring in a string.
#include <string.h>
#include <stdio.h>
void find_str(char const* str, char const* substr)
{
char* pos = strstr(str, substr);
if(pos) {
printf("found the string '%s' in '%s' at position: %d\n", substr, str, pos - str);
} else {
printf("the string '%s' was not found in '%s'\n", substr, str);
}
}
int main(int argc, char* argv[])
{
char* str = "gdeasegrfdtyguhıjohhıhugfydsasdrfgthyjkjhghh";
find_str(str, "hh");
find_str("dhenhheme kekekeke hhtttttttttttttttttttttttttttttttttttttttthhtttttttttttt", "hh");
return 0;
}
Output:
found the string 'hh' in 'gdeasegrfdtyguhıjohhıhugfydsasdrfgthyjkjhghh' at position: 19
found the string 'hh' in 'dhenhheme kekekeke hhtttttttttttttttttttttttttttttttttttttttthhtttttttttttt' at position: 4
In first example it says its in the position 19. But shouldn't it say 18? Because in the second example it says 4. And it starts at the beginning of substring.
I'm confused.
The character 'ı'
is not an ASCII character, so it probably takes up two bytes.
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