各位大佬,我的代码出现次数总是1是为什么呀?

#include
#include
int count=0;
char* str_1[30];
char* str_2[30];

int main(){
printf("输入母字符串:");
scanf("%s",str_1);

printf("输入子字符串:");
scanf("%s",str_2);

int i=0,j=0;

while(*(str_1+i)!='\0'){

    while(*(str_1+i)==*(str_2+j)){
        i++;
        j++;
    }
    count++;
    if(*(str_1+i)!=*(str_2+j)){
        i=i-j+1;
        j=0;
    }

}
printf("子字符串在母字符串中出现的次数为:%d\n",count);
return 0;

}

你是不是没有输入,不输入字符串的话当然就是输出1呀,printf放到子字符串的循环里面更为合理,我是这样认为的。

需要另外弄个变量备份 i 的值,比如使用 k 来备份,下一次比较时,令 i=k+1

#include<stdio.h> 
#include<string.h>
#include<stdlib.h>

int count=0;
char* str_1;
char* str_2;

int main(){
    str_1=(char*)malloc(30*sizeof(char));
    str_2=(char*)malloc(30*sizeof(char));
    printf("输入母字符串:");
    scanf("%s",str_1);
    printf("输入子字符串:");
    scanf("%s",str_2);
    int i=0,j=0,k=0;
    int m=strlen(str_1);
    int n=strlen(str_2);
    while(i<m){
        while(i<m&&j<n&&*(str_1+i)==*(str_2+j)){
            i++;
            j++;
        }
        if(j==n) count++;
        i=++k;
        j=0;
    }
    printf("子字符串在母字符串中出现的次数为:%d\n",count);
    return 0;
}

https://www.cnblogs.com/Legen-da/p/6219531.html