题解 | #统计字符串中子串出现的次数#

统计字符串中子串出现的次数

https://www.nowcoder.com/practice/9eb684f845a446f3b121472de2ea75cd

#include <iostream>
#include <string>
using namespace std;


int main()
{
    char str[100];
    char substr[100];

    cin >> str >> substr ;

    // by find to find the same substring
    string str1(str);
    string str2(substr);
    int postion = 0;
    int count = 0;

    while (str1.find(str2, postion) != string::npos)
    {
        ++count;
        postion = str1.find(str2, postion) + 1;
    }

    cout << count << endl;

    return 0;
}


使用find()函数
全部评论

相关推荐

西松屋:说明原部门有机会把
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客企业服务