33,311
社区成员
发帖
与我相关
我的任务
分享
#include <stdio.h>
int main()
{
char string[50],substr[10];//如果是按照string[50]=" ",substr[10]=" "进行初始化的时候,程序就会出错
int i,j,k,count;
printf("输入字符串:");
gets(string);
printf("输入子串:");
gets(substr);
count=0;
for(i=0;string[i]!='\0';i++)
for(j=i,k=0;string[j]==substr[k];j++,k++)
{
if(substr[k+1]=='\0')
count++;
}
printf("子串出现的次数是:%d\n",count);
}
#include <iostream>
#include <stdlib.h>
using namespace std;
int main()
{
char str1[20] = {0},str2[20] = {0};
char *p1 = NULL,*p2 = NULL;
int sum = 0;
printf("please input two strings\n");
scanf("%s%s",str1,str2);
p1=str1;
p2=str2;
while(*p1!='\0')
{
if(*p1==*p2)
{
while(*p1==*p2&&*p2!='\0')
{
p1++;
p2++;
}
}
else
p1++;
if(*p2=='\0')
sum++;
p2=str2;
}
printf("%d",sum);
system("PAUSE");
return 1;
}