在學習函數(shù)的時候,有很多函數(shù),雖然在平時我們能夠遇見可是有一些函數(shù),卻是我們不了解的,對于它的使用方式更是不甚了解,今天我們就來看一看strstr函數(shù)的具體使用方式是什么?
strstr函數(shù)——strstr函數(shù)是什么意思
strstr(str1,str2)函數(shù)用于判斷字符串str2是否是str1的子串。如果是,則該函數(shù)返回str2在str1中首次出現(xiàn)的地址;否則,返回NULL。
strstr(string,search [, bool $before_needle = false ])
注釋:search若是數(shù)字,所搜索的將是該數(shù)字(作為ASCII碼)代表的字符。
注釋:該函數(shù)是二進制安全的。
注釋:該函數(shù)對大小寫敏感。如需進行大小寫不敏感的搜索,請使用stristr()。
Strstr函數(shù)如何自己實現(xiàn)
請用標準C語言實現(xiàn)下列標準庫函數(shù),設計中不得使用其他庫函數(shù)。char*strstr(char*str1,char*str2);在字符串str1中,尋找字串str2,若找到返回找到的位置,否則返回NULL。[cpp]viewplaincopy#include#includeusingnamespacestd;&
請用標準C語言實現(xiàn)下列標準庫函數(shù),設計中不得使用其他庫函數(shù)。char*strstr(char*str1,char*str2);
在字符串str1中,尋找字串str2,若找到返回找到的位置,否則返回NULL。
[cpp]viewplaincopy
#include
#include
usingnamespacestd;
constchar*StrStr(constchar*str1,constchar*str2)
{
assert(NULL!=str1&;&;NULL!=str2);
while(*str1!=’/0′)
{
constchar*p=str1;
constchar*q=str2;
constchar*res=NULL;
if(*p==*q)
{
res=p;
while(*p&;&;*q&;&;*p++==*q++)
;
if(*q==’/0′)
returnres;
}
str1++;
}
returnNULL;
}
intmain()
{
constchar*str1=”wangyang”;
constchar*str2=”ang”;
constchar*res=StrStr(str1,str2);
if(res!=NULL)
cout<
else
cout<<“NOT”<
system(“pause”);
}
以上是strstr函數(shù)的自己實現(xiàn)的內容,更多函數(shù)的內容,歡迎關注優(yōu)詞網(wǎng)。最后也需要提醒各位的是,在使用函數(shù)的時候,一定要按照函數(shù)的正確方式和正確的步驟來操作,否則結果是很容易出錯的。