

題目
解決代碼及點評
/*寫strstr函數簡單的遍歷去查找吧
*/#include <iostream>
#include <stdio.h>const char *my_strstr(const char *str, const char *sub_str)
{// 遍歷for(int i = 0; str[i] != '\0'; i++){int tem = i; //tem保留主串中的起始判斷下標位置 int j = 0;// 與substr去匹配while(str[i++] == sub_str[j++]){// 如果匹配成功if(sub_str[j] == '\0'){// 返回成功位置return &str[tem];}}i = tem;}return NULL;
}int main()
{char *s = "1233345hello";char *sub = "345";printf("%s\n", my_strstr(s, sub));system("pause");return 0;
}
代碼下載及其運行
代碼下載地址:http://download.csdn.net/detail/yincheng01/6704519
解壓密碼:c.itcast.cn
下載代碼并解壓后,用VC2013打開interview.sln,并設置對應的啟動項目后,點擊運行即可,具體步驟如下:
1)設置啟動項目:右鍵點擊解決方案,在彈出菜單中選擇“設置啟動項目”
2)在下拉框中選擇相應項目,項目名和博客編號一致
3)點擊“本地Windows調試器”運行
程序運行結果