#include <stdio.h>
#include <string.h>
int string_find( char str[], char substr[] )
{
int i, j, check ,count = 0;
int len = strlen( str ); /*取得字符串长度,不包括'\0'*/
int sublen = strlen( substr );
for( i = 0; i < len; i++ )
{
check = 1; /*检测标记*/
for( j = 0; j + i < len && j < sublen; j++ ) /*逐个字符进行检测,在sublen长度内,一旦出现不同字符便将check置为0*/
{
if( str[i + j] != substr[j] )
{
check = 0;
break;
}
}
if( check == 1 )/*在sublen长度内的字符都相等*/
{
count++;
i = i + sublen; /*调整检测起始位置*/
}
}
return count;
}
int main()
{
char str[100];
char word[100];
gets(str);
gets(word);
int count;
count=string_find(str,word);
printf("%d",count);
return 0;
}
查找字符串中的子串数目
最新推荐文章于 2022-04-03 13:42:16 发布