题解 | #统计字符串中子串出现的次数#
统计字符串中子串出现的次数
https://www.nowcoder.com/practice/9eb684f845a446f3b121472de2ea75cd
#include <iostream> #include <string.h> using namespace std; #include <algorithm> int my_strstr(const char* str1, const char* str2) { const char* s1 = NULL; const char* s2 = NULL; const char* cp = str1; if (*str2 == '\0') { return 0; } int count=0; while (*cp) { s1 = cp; s2 = str2; while (*s1 && *s2 && (*s1 == *s2)) { s1++; s2++; } if (*s2 == '\0') { count++; } cp++; } return count; } int main() { char str[100] = { 0 }; char substr[100] = { 0 }; cin.getline(str, sizeof(str)); cin.getline(substr, sizeof(substr)); int ret=my_strstr(str,substr); cout<<ret; return 0; }