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

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

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

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

int main() {

    char str[100] = { 0 };
    char substr[100] = { 0 };

    cin.getline(str, sizeof(str));
    cin.getline(substr, sizeof(substr));

    int count = 0;
    int len=strlen(str),len1=strlen(substr);
    for(int k = 0;k<len;k++)
    {
        if(str[k]==substr[0])
        {
            int tmp=k;
            for(int j=0;j<len1;j++)
            {
                if(str[tmp]==substr[j])
                {
                    if(substr[j+1]=='\0')
                    {
                        count++;
                    }
                    tmp++;
                }
                else {
                    break;
                }
                
            }
        }
    }
    // write your code here......
    

    cout << count << endl;

    return 0;
}

全部评论

相关推荐

01-23 19:12
门头沟学院 Java
榨出爱国基因:你还差 0.1% 就拿到校招礼盒,快叫朋友给你砍一刀吧
投递拼多多集团-PDD等公司10个岗位
点赞 评论 收藏
分享
不愿透露姓名的神秘牛友
02-12 10:05
小米集团 算法工程师 28.0k*15.0
泡沫灬一触即破:楼上那个看来是看人拿高薪,自己又不如意搁这泄愤呢是吧,看你过往评论很难不怀疑你的精神状态
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客企业服务