亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

字符串匹配問題:輸入一個字符串,如何計算其中包含的連續給定的子字符串的個數?

字符串匹配問題:輸入一個字符串,如何計算其中包含的連續給定的子字符串的個數?

料青山看我應如是 2022-05-07 11:07:31
例如輸入字符串“ EFABCABCABCDABCDD ” , 給定子字符串“ ABC” ,輸出是 3 。函數原型: int countsub( char *str, char *subs ) 。參數說明: str 保存輸入的字符串的首地址, subs 保存需要統計的子字符串的首地址。返回值:包含的連續子字符串的個數。預設代碼countsub_H20.cview plaincopy to clipboardprint?/* PRESET CODE BEGIN - NEVER TOUCH CODE BELOW */#include <stdio.h>int countsub( char *str, char *ss );main( ){char s1[1000] = {0}, s2[100] = {0};gets(s1);gets(s2);printf("%d\n", countsub( s1, s2 ) );}/* PRESET CODE END - NEVER TOUCH CODE ABOVE */急求!!謝謝??!
查看完整描述

2 回答

?
翻閱古今

TA貢獻1780條經驗 獲得超5個贊

#include<stdio.h>
int countsub(char *str, char *ss);
int main(void)
{
char s1[1000] = { 0 }, s2[100] = { 0 };
gets(s1);
gets(s2);
printf("%d\n", countsub(s1, s2));
return 0;
}
int countsub(char *str, char*ss)
{
int i=0; //記錄有多少相同的字符串。
char *s22 = ss; //標記ss字符串的原點
while (*str != '\0')
{
while (*str == *ss)
{
if (*str == '\0')
{
break;
}
str++;
ss++;
}
if (*ss == '\0')
{
i++;
}
else{
str++;
}
ss = s22;
}
return i;
}



查看完整回答
反對 回復 2022-05-09
?
慕桂英4014372

TA貢獻1871條經驗 獲得超13個贊

#include<iostream>

using namespace std;
int size(char *sz)
{
int i = 0;
while (sz[++i] != NULL);
return i;
}
int countsub(char *str, char *ss)
{
int temp = 0; int count = 0;
if (ss == NULL || str == NULL) return -1;
int strLen = size(str);
int ssLen = size(ss);
for (int i = 0; i<strLen; i++)
{
if (str[i] == ss[temp])
{
temp++;
}
else if (str[i] == ss[0])
{
temp = 1;
}
else
{
temp = 0;
}
if (temp == ssLen)
{
count++;
temp = 0;
}
}
return count;
}

int main()
{
char op[2][50] = { {'a','s',
'a','s',
'a','s',
'a','s',
'a',
'a','s'}, {'a','s'} };
cout << countsub(op[0],op[1])<<endl;
}



查看完整回答
反對 回復 2022-05-09
  • 2 回答
  • 0 關注
  • 669 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號