2 回答
TA贡献1780条经验 获得超5个赞
#include<stdio.h>
int countsub(char *str, char *ss);
int main(void)
{
char s1[1000] = { 0 }, s2[100] = { 0 };
gets(s1);
gets(s2);
printf("%d\n", countsub(s1, s2));
return 0;
}
int countsub(char *str, char*ss)
{
int i=0; //记录有多少相同的字符串。
char *s22 = ss; //标记ss字符串的原点
while (*str != '\0')
{
while (*str == *ss)
{
if (*str == '\0')
{
break;
}
str++;
ss++;
}
if (*ss == '\0')
{
i++;
}
else{
str++;
}
ss = s22;
}
return i;
}
TA贡献1871条经验 获得超13个赞
#include<iostream>
using namespace std;
int size(char *sz)
{
int i = 0;
while (sz[++i] != NULL);
return i;
}
int countsub(char *str, char *ss)
{
int temp = 0; int count = 0;
if (ss == NULL || str == NULL) return -1;
int strLen = size(str);
int ssLen = size(ss);
for (int i = 0; i<strLen; i++)
{
if (str[i] == ss[temp])
{
temp++;
}
else if (str[i] == ss[0])
{
temp = 1;
}
else
{
temp = 0;
}
if (temp == ssLen)
{
count++;
temp = 0;
}
}
return count;
}
int main()
{
char op[2][50] = { {'a','s',
'a','s',
'a','s',
'a','s',
'a',
'a','s'}, {'a','s'} };
cout << countsub(op[0],op[1])<<endl;
}
添加回答
举报