C语言问题计算字符串中子串出现的次数。要求:用一个子函数subString()实现,参数为指向字符串
1个回答
展开全部
说明:自己看代码:int subString(const char *inChar,int inSize,const char *subChar,int subSize){
int i=0,j=0,iCount = 0,INSIZE=inSize-1,SUBSIZE=subSize-1;
while(INSIZE>SUBSIZE)
{
while(i<SUBSIZE)
{
if((inChar[i+inSize-INSIZE]==subChar[i]))
{
j+=1;
printf("the same %c - %c index:%d\n",inChar[i+inSize-INSIZE],subChar[i],i+inSize-INSIZE);
if(j==SUBSIZE){
iCount+=1;
printf("get it (%d) times\n\n",iCount);
}
}
else
{
printf("not same %c - %c\n",inChar[i+inSize-INSIZE],subChar[i]);
}
i++;
}
i=j=0;
INSIZE--;
}
return iCount;}
int _tmain(int argc, _TCHAR* argv[]){
char inChar[]="122345622234772234577772232457";
char subChar[]="23";
printf("Find times :%d",subString(inChar,sizeof(inChar),subChar,sizeof(subChar)));
return 0;
}
下面是运行结果:
the same 2 - 2 index:1the same 2 - 2 index:2
the same 3 - 3 index:3
get it (1) times
the same 2 - 2 index:7the same 2 - 2 index:8
the same 2 - 2 index:9
the same 3 - 3 index:10
get it (2) times
the same 2 - 2 index:14the same 2 - 2 index:15
the same 3 - 3 index:16
get it (3) times
the same 2 - 2 index:23the same 2 - 2 index:24
the same 3 - 3 index:25
get it (4) times
the same 2 - 2 index:26
Find times :4
int i=0,j=0,iCount = 0,INSIZE=inSize-1,SUBSIZE=subSize-1;
while(INSIZE>SUBSIZE)
{
while(i<SUBSIZE)
{
if((inChar[i+inSize-INSIZE]==subChar[i]))
{
j+=1;
printf("the same %c - %c index:%d\n",inChar[i+inSize-INSIZE],subChar[i],i+inSize-INSIZE);
if(j==SUBSIZE){
iCount+=1;
printf("get it (%d) times\n\n",iCount);
}
}
else
{
printf("not same %c - %c\n",inChar[i+inSize-INSIZE],subChar[i]);
}
i++;
}
i=j=0;
INSIZE--;
}
return iCount;}
int _tmain(int argc, _TCHAR* argv[]){
char inChar[]="122345622234772234577772232457";
char subChar[]="23";
printf("Find times :%d",subString(inChar,sizeof(inChar),subChar,sizeof(subChar)));
return 0;
}
下面是运行结果:
the same 2 - 2 index:1the same 2 - 2 index:2
the same 3 - 3 index:3
get it (1) times
the same 2 - 2 index:7the same 2 - 2 index:8
the same 2 - 2 index:9
the same 3 - 3 index:10
get it (2) times
the same 2 - 2 index:14the same 2 - 2 index:15
the same 3 - 3 index:16
get it (3) times
the same 2 - 2 index:23the same 2 - 2 index:24
the same 3 - 3 index:25
get it (4) times
the same 2 - 2 index:26
Find times :4
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询