c 語言 計算字串中子字串出現的次數

時間 2021-08-11 17:35:06

1樓:匿名使用者

1.可通過 strstr 函式,查詢子字串。找到後即非空,然後加上子字串偏移,再進行查詢沒,直到最後返回為空。

2.char *strstr( const char *str1, const char *str2 );

功能:函式返回一個指標,它指向字串str2首次出現於字串str1中的位置,如果沒有找到,返回null。

#include

#include

// 從str1中查詢str2的個數,並返回int findchildcnt(char* str1, char* str2)

return cnt;

}int main()

2樓:匿名使用者

while((*p1=*p2)&&(*p2!='\0'))

裡面把==寫成=了,是*p1==*p2

3樓:匿名使用者

//還可以簡單點

#include

using namespace std;

int main()

if(*pd=='\0')

sum++;

pd=sd;

pc++;

}cout<

4樓:輝騰無限

#include

#include

using namespace std;

int main() }

else p1++;

if(*p2=='\0') sum=sum+1;

p2=str2;

} cout<

#include

using namespace std;

int main() }

p1++;

if(*p2=='\0') sum=sum+1;

p2=str2;

} cout<

return 0;}

c++統計某字串中某指定字元出現的次數。

5樓:雲南新華電腦學校

#include

using namespace std;

int a[26];//大寫

來int a[26];//小寫

int main()

for(int i=0;i<=26;i++)if(a[i]!=0)cout<暫停(按任意鍵繼續),比賽中不要加這句

return 0;}

c++中一個子字串在另一個字串中出現的次數

6樓:匿名使用者

#include

using std::cout;

using std::endl;

//函式1: 查詢子串sub在str中出現的次數--用c++方式--子串可重疊

int fun1(const std::string& str, const std::string& sub)

//函式2: 查詢子串sub在str中出現的次數--用c方式--子串可重疊

int fun2(const char* str, const char* sub)

//函式3: 查詢子串sub在str中出現的次數--用c++方式--子串不重疊

int fun3(const std::string& str, const std::string& sub)

//函式4: 查詢子串sub在str中出現的次數--用c方式--子串不重疊

int fun4(const char* str, const char* sub)

void main()

c++中怎樣計算一個字串中其中一個字元出現的次數

7樓:

每個字元都有對應的ascii值,可以選定一個字元後對其他字元比較,如果值相同則計數+1,這樣遍歷一次就能計算出

8樓:

# include

#include

#include

using namespace std;

int main()

9樓:夢羽天堂

用一個迴圈比如while(a[i]!='\0')然後用switch語句判斷每一個元素出現的次數,用一個變數自加來計數。

c#中計算字串中子串出現的次數

10樓:非常可愛

classprogram

次;索引是",count,index);

index=index+keyword.length;

}console.writeline("朋友出現的總次數:",count);

console.readkey();}}

擴充套件資料c#迴圈遍歷字串中某字元出現的次數

stringstr="asdwqwe@dsfad@sadfwer@@@fwed";

intc1=0;

for(inti=0;i

}messagebox.show(c1.tostring());

11樓:極度獸性

string str= "朋友一生一起走,那些日子不再有,一句話一輩子,一生情一杯酒.朋友不曾孤單過回,一聲朋友你會懂,還有傷答還有痛,還要走還有我";

response.write(system.text.regularexpressions.regex.matches(str,@"朋友").count);

12樓:匿名使用者

樓上抄的思路比較複雜。來個簡單的如下:

public static int countsubstr(string str, string sub)

就是用這個子串做標誌,把原字串分割成陣列,字串的數量就是陣列長度-1

13樓:匿名使用者

int count = 0;

string str = "sfsfluljslfnslafuensdfre";

foreach(char c in str)}或者string str = "sfsfluljslfnslafuensdfre";

string s="s";

int count= str.length - str.replace(s,"").length;

14樓:匿名使用者

string str = "---123---123---123";

string substr = "123";

var rgx = regex.matches(str, substr);

console.writeline("出現次du數zhi

dao:" + rgx.count);

出現次數:3

使用正則回來搞答

c++程式設計:輸入一個字串,統計一長度不超過2的子串在該字串中出現的次數.

15樓:我是比丘猴

int main()

cout <<"搜尋

的字元段出回現次數為:"<< num << endl;

return 0;

} //輸入一段字元段,再輸答入一段小的字元段,匯出小字元段出現次數是這個吧,希望樓主能看懂哈~

16樓:匿名使用者

c語言用函式程式設計實現計算字串中子串出現的次數。為什麼子串是一位數或者出現1的時候輸出不正確,求修改

17樓:匿名使用者

if(str[i+j]==sub[j]) 當i+j>=m的時候指標溢位了。修改函式如下:

int findstring(char *str,char *sub)

if(wow==n)

count++;

wow=0;

}return count;}

18樓:匿名使用者

char 型別怎麼可以用等號if(str[i+j]==sub[j]) 來進行比較呢。應該用strcmp吧

c語言,字串,C語言中,字串是什麼?

c語言中,字串是什麼?位操作運算都只能是整形或字元型,字元型實際是單位元組整形。包括以下幾個操作符 按位與 按位或。按位異或。左移 右移 另外還有一個取餘操作 a b的意思是,a除以b,取餘數。這個是c語言的硬性規定,位操作都是正數按照二進位制進行表達的時候才有的概念 而取餘操作是整數相除得餘數,餘...

c語言刪除字串中的字元,C語言 刪除字串中指定的字元

受災 這個容易,就是把指定位置的字元去掉,也就是說後面的字元覆蓋比如刪除指定的字元在字串中第一個出現的位置void strdel char str,char ch if p 希望能解決您的問題。 微俊 include include int main s i 0 printf input a cha...

c語言反轉字串怎麼反轉,C語言反轉字串怎麼反轉?

讀取字串,瀏覽字串每一個字元,如果是空格就輸出空格,否則就儲存當前位置,從當前位置移動到下一個空格或字元尾前,從當前位置開始反向輸出,直達回到儲存的位置最後輸出回車,結束 以下是c語言版的,需要的話,可以看看 include include include define string maxsize...