做网站运营如何提升用户粘度,crm 都免费了,海南做网站,网站分享的功能怎么做实例要求#xff1a;
1、请你实现C库函数strstr()#xff08;stdio.h string.h#xff09;#xff0c;请在 haystack 字符串中找出 needle 字符串的第一个匹配项的下标#xff08;下标从 0 开始#xff09;#xff1b;2、函数声明#xff1a;int strStr(char* h…实例要求
1、请你实现C库函数strstr()stdio.h string.h请在 haystack 字符串中找出 needle 字符串的第一个匹配项的下标下标从 0 开始2、函数声明int strStr(char* haystack, char* needle)参数1、haystack -- 被检索的字符串2、needle -- haystack字符串内要匹配的子字符串返回值1、函数strStr()返回在 haystack中第一次出现 needle 字符串的位置2、在 haystack中未找到则返回-1
案例展示 实例分析
1、利用strlen函数分别求出字符串haystack和needle的长度 2、双重for嵌套循环遍历两个字符串3、设置标志位flag初值为04、当flag等于0时返回第一个匹配项的下标5、其他的情况则返回-1
示例代码 int strStr(char* haystack, char* needle) {int len1 strlen(haystack);int len2 strlen(needle);int i 0;int j 0;int flag 0;for(i 0; i len1; i){flag 0;for(j 0; j len2; j){if(haystack[ij] ! needle[j]){flag 1;break;}}if(flag 0){return i;}}return -1;}
运行结果