1. 程式人生 > >字串操作函式之strstr和strpbrk

字串操作函式之strstr和strpbrk

在字串操作函式中,除了經常用的連線、拷貝、求長度等,今天我們來說說可以在一個字串裡查詢另外一個字串的函式:strstr和strpbrk函式。定義如下:

 #include <string.h>
  char *strstr(const char *haystack, const char *needle);
  char *strpbrk(const char *s, const char *accept);

二者在呼叫成功都會返回查詢的字串位置,失敗則返回NULL。
strstr是在haystack中查詢needle字串所在的位置,strpbrk則是在s中查詢accept中任意第一個字元出現的位置。
程式碼實現:

這裡寫圖片描述
執行結果:
這裡寫圖片描述