B字符串在A字符串中出现的次数

来源:互联网 发布:在线考试php源码 编辑:程序博客网 时间:2024/06/16 03:46

转载自http://hi.baidu.com/liuflin/blog/item/786578cc632fbe1700e928d2.html

输入两个字符串,分别命名为串A和串B,现要查找出,B在A中出现了多少次,并且输出次数。如ababa和aba,

前者是串A,后者是串B,B在A中出现了两次。

 

#include "stdafx.h"
#include "string.h"

int main(int argc, char* argv[])
{
char strA[80],strB[80];
int count = 0;
//printf("Pleace input strA: ");
scanf("%s", &strA);
//printf("Pleace input strB: ");
scanf("%s", &strB);
char *p;
p = strstr(strA, strB);
while (p)
{
   count++;
   p++;
   p = strstr(p, strB);
}
printf("%d/n", count);
return 0;
}

原创粉丝点击