编程知识 cdmana.com

There are two methods to write the number of sub strings of the main string. One is an ordinary function and the other is recursion. I want to ask how to change recursion

#include <stdio.h>

#include <string.h>
int occurrence1(char *sub, char *str);
int occurrence2(char *sub, char *str);
int main()
{
char str1[20], str2[80];
scanf("%s%s", str1, str2);
printf("%d\n", occurrence1(str1, str2));
printf("%d\n", occurrence2(str1, str2));
return 0;
}
int occurrence1(char *sub, char *str)
{
int i,j;
for(i=0;i<strlen(str);i++)
{
for(j=0;j<strlen(sub);j++)
{
if(str[i+j]!=sub[j])
break;
}
}
if(j==strlen(sub))
{
i++;
return occurrence1(sub,str)+1;
}
else
return 0;
}
int occurrence2(char *sub, char *str)
{
int i,j,count=0;
for(i=0;i<strlen(str);i++)
{
for(j=0;j<strlen(sub);j++)
{
if(str[i+j]!=sub[j])
break;
}
if(j==strlen(sub))
count++;
}
return count;
}

版权声明
本文为[CSDN Q & A]所创,转载请带上原文链接,感谢
https://cdmana.com/2021/12/20211207175245182z.html

Scroll to Top