Implement strStr().
Returns the index of the first occurrence of needle in haystack, or -1 if needle is not part of haystack.
实现函数strStr。代码如下:
int strStr(char* haystack, char* needle) { size_t len = strlen(haystack); if(strlen(needle) == 0) return 0; if(strcmp(haystack, needle) == 0) return 0; for(int i=0; i<len;i++){ if(haystack[i] == needle[0]){ if(strncmp(&(haystack[i]), needle, strlen(needle)) == 0) return i; } } return -1; }
时间: 2024-10-21 04:54:59