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

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

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

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

int main() {

    string str;
    string substr;
    cin>>str;
    cin>>substr;
    int count = 0;
    for(int i=0,n=0;i<str.size();i++)
    {
        string a;//用于存放在str中获取的substr.size()大小的字符串
        n=i;//用于选择从str中每个字符开始累加
        for(int j=0;j<substr.size();j++)
        {
            a+=str[n];//开始累加
            n++;
        }
        if(substr==a)//判断两字符串是否相等
            count++;
    }
    cout << count << endl;
    return 0;
}

全部评论

相关推荐

下北澤大天使:你是我见过最美的牛客女孩😍
点赞 评论 收藏
分享
零OFFER战士:另一个版本查看图片
点赞 评论 收藏
分享
评论
1
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务