C语言:计算字符串中子串出现的次数 程序说明:利用输入函数输入任意两个字符串,请你编写程序求出第二个字符串在第一个字符串中出现的次数,即在第一个字符串中有几个第二个字符串。

#include<stdio.h>
int strCount(char * str1, char * str2)
{
    
    
	int count = 0;
	for(int i = 0;str1[i]!='\0';i++)
	{
    
    
		int j = 0;
		for(j = 0;str1[i+j]!='\0'&&str2[j]!='\0';j++)
	{
    
    
		if(str1[i+j]!=str2[j])
		break;
	}
	if(str2[j] == '\0')
	count++;
	}
	return count;
 } 
 int main()
 {
    
    
 	char a[1000];
 	char b[1000];
 	printf("请输入第一个字符串:\n");
 	scanf("%s",&a);
 	printf("请输入第二个字符串:\n");
 	scanf("%s",&b);
 	printf("出现次数为:\n");
 	printf("%d\n",strCount(a,b));
 	return 0;
 }

对我的博客或其他方面有任何见解或问题的话都可以私信我
或者联系本人QQ:3128909688
微信:DreamHerome
欢迎致电

猜你喜欢

转载自blog.csdn.net/RViewSonic/article/details/108321740