计算字符串中子串出现的次数
题目要求:
利用输入函数输入任意两个字符串,请编写程序求出第二个字符串在第一个字符串中出现的次数,即在第一个字符串中有几个第二个字符串。
主要功能包括:
(1) 用菜单界面实现;
(2) 输入主串;
(3) 输入子串;
(4) 输出子串在主串中出现的次数;
(5) 退出系统;
C语言课程设计题目计算字符串中子串出现的次数
答案:2 悬赏:70
解决时间 2021-02-20 12:33
- 提问者网友:失败的占卜者
- 2021-02-19 22:14
最佳答案
- 二级知识专家网友:狠傷凤凰
- 2021-02-19 22:53
//题意中的,菜单界面指的是什么?下面的code是之前写的
#include<stdio.h>
//计算子串在父串中出现的次数
int strCount(char * str, char * sFind){
int count = 0;
for(int i = 0; str[i]!='\0';i++){
int j = 0;
for(j = 0; str[i+j]!='\0'&&sFind[j]!='\0';j++){
if(str[i+j]!=sFind[j])
break;
}
if(sFind[j] == '\0')
count++;
}
return count;
}
int main(){
char a[1000];
char b[1000];
scanf("%s %s", a, b);
printf("%d\n", strCount(a, b));
return 0;
}
#include<stdio.h>
//计算子串在父串中出现的次数
int strCount(char * str, char * sFind){
int count = 0;
for(int i = 0; str[i]!='\0';i++){
int j = 0;
for(j = 0; str[i+j]!='\0'&&sFind[j]!='\0';j++){
if(str[i+j]!=sFind[j])
break;
}
if(sFind[j] == '\0')
count++;
}
return count;
}
int main(){
char a[1000];
char b[1000];
scanf("%s %s", a, b);
printf("%d\n", strCount(a, b));
return 0;
}
全部回答
- 1楼网友:许你一世温柔
- 2021-02-20 00:27
#include "string.h" #include "stdio.h" main() { char str1[20],str2[20],*p1,*p2; int sum=0; printf("please input two strings\n"); scanf("%s%s",str1,str2); p1=str1;p2=str2; while(*p1!='\0') { if(*p1==*p2) {while(*p1==*p2&&*p2!='\0') {p1++; p2++;} } else p1++; if(*p2=='\0') sum++; p2=str2; } printf("%d",sum); getch();}
我要举报
如以上问答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
推荐资讯