Reputation: 2067
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.
Upvotes: 4
Views: 601
Reputation: 409136
The character 'ı'
is not an ASCII character, so it probably takes up two bytes.
Upvotes: 9