实现strstr功能,即在父串中寻找子串首次出现的位置。(笔试中常让面试者实现标准库中的一些函数)
实现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));
}