溫馨提示×

c語言strstr函數的作用是什么

小億
129
2024-02-22 11:29:25
欄目: 編程語言

在C語言中,strstr函數用于在一個字符串中查找另一個字符串第一次出現的位置。該函數的原型如下:

char *strstr(const char *haystack, const char *needle);

其中,haystack是要查找的字符串,needle是要查找的子字符串。該函數會返回一個指向第一次出現needle的位置的指針,如果未找到則返回NULL。

例如,如果有字符串"hello world",要查找其中是否包含字符串"world",可以使用strstr函數:

char *result = strstr("hello world", "world");
if (result != NULL) {
    printf("Found at position: %ld\n", result - "hello world");
}

上述代碼會輸出Found at position: 6,表示在字符串"hello world"中找到了子字符串"world",并且是從位置6開始的。

0
亚洲午夜精品一区二区_中文无码日韩欧免_久久香蕉精品视频_欧美主播一区二区三区美女