Posted in 面试题 onAugust 05, 2016
实现strstr功能,即在父串中寻找子串首次出现的位置。(笔试中常让面试者实现标准库中的一些函数)
char * strstring(char *ParentString, char *SubString) {
char *pSubString, *pPareString;
for(char *pTmp=ParentString; *pTmp; pTmp++) {
pSubString = SubString;
pPareString = pTmp;
while(*pSubString == *pPareString && *pSubString != ‘\0′) {
pSubString++;
pPareString++;
}
if(*pSubString == ‘\0′) return pTmp;
}
return NULL;
}
int main(int argc, char* argv[]) {
char *ParentString = “happy birthday to you!”;
char *SubString = “birthday”;
printf(“%s”,strstring(ParentString, SubString));
}
char * strstring(char *ParentString, char *SubString) {
char *pSubString, *pPareString;
for(char *pTmp=ParentString; *pTmp; pTmp++) {
pSubString = SubString;
pPareString = pTmp;
while(*pSubString == *pPareString && *pSubString != ‘\0′) {
pSubString++;
pPareString++;
}
if(*pSubString == ‘\0′) return pTmp;
}
return NULL;
}
int main(int argc, char* argv[]) {
char *ParentString = “happy birthday to you!”;
char *SubString = “birthday”;
printf(“%s”,strstring(ParentString, SubString));
}
实现strstr功能,即在父串中寻找子串首次出现的位置
声明:登载此文出于传递更多信息之目的,并不意味着赞同其观点或证实其描述。
Tags in this post...
Reply on: @reply_date@
@reply_contents@