Strange strstr behavior

I am trying to find the position of a 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 string 'hh' in 'dhenhheme kekekeke hhtttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttt 4

In the first example, he talks about his position in position 19. But shouldn't it be 18? Because in the second example, he says 4. And starts at the beginning of the substring.

I am embarrassed.

+4
source share
1 answer

The character is 'ı'not an ASCII character, so it probably takes two bytes.

+9

All Articles