型:extern char *strstr(char *haystack, char *needle);
用法:#include <string.h>
功能:從字符串haystack中尋找needle第一次出現的位置(不比較結束符NULL)。
說(shuō)明:返回指向第一次出現needle位置的指針,如果沒(méi)找到則返回NULL。
舉例:
// strstr.c
#include <syslib.h>
#include <string.h>
main()
{
char *s="Golden Global View";
char *l="lob";
char *p;
clrscr();
p=strstr(s,l);
if(p)
printf("%s",p);
else
printf("Not Found!");
getchar();
return 0;
}
語(yǔ)法:int strstr(str1,str2)
str1: 被查找目標 string expression to search.
str2:要查找對象 The string expression to find.
該函數返回str2第一次在str1中的位置,如果沒(méi)有找到,返回NULL
The strstr() function returns the ordinal position within str1 of the first occurrence of str2. If str2 is not found in str1, strstr() returns 0.
例子:
功能:從字串” string1 onexxx string2 oneyyy”中尋找”yyy”
(假設xxx和yyy都是一個(gè)未知的字串)
char *s=” string1 onexxx string2 oneyyy”;
char *p;
p=strstr(s,”string2”);
if(!p) printf(“Not Found!”);
p=strstr(p,”one”);
if(!p) printf(“Not Found!”);
p+=strlen(“one”)
printf(“%s”,p);
說(shuō)明:如果直接寫(xiě)語(yǔ)句p=strstr(p,”one”),則找到的是xxx,不符合要求
所以需采用二次查找法找到目標
本站僅提供存儲服務(wù),所有內容均由用戶(hù)發(fā)布,如發(fā)現有害或侵權內容,請
點(diǎn)擊舉報。