Search

9/02/2006

strchr

譬如要找在Hello World中W出現在第幾個char, 注意p-target的部分

int main() {
char target[] = "Hello World", *p;
 
p = strchr(target, 'W');
printf("The character W was found at position %d\n", p-target);
}

The character W was found at position 6

或者用strcspn, 注意這邊"W"是雙引號,因為第二個參數是個char *而不是char
int main()
{
char target[] = "Hello World";
int position;
 
position = strcspn(target, "W");
printf("The character W was found at position %d\n", position);
}

The character W was found at position 6

類似的還有strpbrk, 跟strchar在於第二個函式是個char *, 可以match多個char

沒有留言: