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

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

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 str_len  = strlen(str);
    int substr_len  = strlen(substr);
    int count = 0;
    for(int i=0; i<str_len; i++){
        bool flag = true;
        for(int j=0; j<substr_len; j++){
            if(str[i+j] == substr[j]){
                continue;
            }else{
                flag = false;
                break;
            }
    }
        if(flag){
            count++;
        }
    }


    cout << count << endl;

    return 0;
}

str[i+j] == substr[j]是解题的关键,要求两个字符串一起动

C/C++题解 文章被收录于专栏

记录个人编程题的解题思路以及学习的新知识

全部评论

相关推荐

01-11 08:47
门头沟学院 Java
choumoduji...:读研的目的就是为了以最快的速度和最低的要求完成“学校”规定的毕业标准,而不是所谓课题组的要求
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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